First, preparatory work
1. Create the table to be read in MongoDB
Create DATABASE Mongotest
Copy Code code as follows:
Inserting data into the user table
Copy Code code as follows:
Db.user.insert ({
Name: ' Flyoung ',
Age: ' 18 ',
Sex:true
});
2. Installation Node-mongodb-native
Copy Code code as follows:
Ii. examples (Node.js read MongoDB)
Refer to Node-mongodb-native's documentation: https://github.com/mongodb/node-mongodb-native
Copy Code code as follows:
var MongoDB = require (' MongoDB ');
var server = new MongoDB. Server ("127.0.0.1", 27017,{});//local 27017 port
New MongoDB. Db (' Mongotest ', server,{}). Open (function (error,client) {//database: Mongotest
if (error) throw error;
var collection = new MongoDB. Collection (client, ' user ');//table: User
Collection.find (function (error,cursor) {
Cursor.each (function (Error,doc) {
if (DOC) {
Console.log ("Name:" +doc.name+ "Age:" +doc.age);
}
});
});
});
Run:
Copy Code code as follows:
Results:
Copy Code code as follows:
Three, write in the last words
Check and delete the demo reference document