Home > Web Front-end > JS Tutorial > body text

How to query a piece of data in node.js

藏色散人
Release: 2021-12-16 11:46:45
Original
2095 people have browsed it

Node.js method to query a piece of data: 1. Create relevant js files; 2. Query a piece of data through the code "const MongodbClient = require("mongodb").MongoClient,assert = require..." Can.

How to query a piece of data in node.js

The operating environment of this article: windows7 system, mongodb nodeJS driver version 3.2.3, Dell G3 computer.

node.js How to query a piece of data?

NodeJs uses mongodb to query a piece of data

源代码请查看GitHub:https://github.com/slhuang520/study/tree/master/web/nodeJS/mongoDB/findOne.js
Copy after login

The currently used mongodb nodeJS driver version is 3.2.3,

Different Driver versions, the methods are very different difference.

When querying a piece of data, you can also use sorting or certain conditions.

const MongodbClient = require("mongodb").MongoClient,
    assert = require("assert");
(async () => {
    const url = "mongodb://127.0.0.1:27017",
        dbName = "mydb",
        client = new MongodbClient(url);
    try {
        await client.connect();
        const db = client.db(dbName);
        let col = await db.collection("user");
        col.findOne().then(res => {
            console.log(res); //{ _id: 5ccd913ef78b417285224075, name: 'Jim', age: 12 }
        });
    } catch (e) {
        console.log(e.stack);
    } finally {
        client.close();
    }
})();
Copy after login

Recommended learning: "node.js video tutorial"

The above is the detailed content of How to query a piece of data in node.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template