First, the preparatory work
1. Create the table to be read in MongoDB
Create a database Mongotest
Use Mongotest;
Inserting data into the user table
Db.user.insert ({name: ' Flyoung ', Age: ' + ', sex:true});
2, Installation Node-mongodb-native
NPM Install MongoDB
Ii. instances (node. js reads MongoDB)
Documentation for reference node-mongodb-native: https://github.com/mongodb/node-mongodb-native
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 (Error, DOC) { if (doc) { console.log ("Name:" +doc.name+ "Age:" +doc.age); } );});
Run:
Node Mongodbtest.js
Results:
Name:flyoung age:18
Three, written in the last words
The demo reference document for adding and deleting changes
Article Source: node. js combined with MongoDB
node. js combined with MongoDB