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.
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
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(); } })();
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!