Link database:
varMongo=require ("MongoDB"); varhost= "localhost"; varport=MONGO. Connection.default_port; varServer=NewMongo. Server (host,port,{auto_reconnect:true});//Server server where the database is created vardb=NewMongo. Db ("Node-mongo-examples", Server,{safe:true});//To Create a database objectDb.open (function(ERR,DB) {//connecting to a database if(ERR)Throwerr; Else{Console.log ("Database connection successfully established"); Db.close (); } }); Db.on ("Close",function(ERR,DB) {//Close the database if(ERR)Throwerr; ElseConsole.log ("Successfully closed database.")); });
Insert Data :
Output data document content in console after inserting data
varMongo=require ("MongoDB"); varhost= "localhost"; varport=MONGO. Connection.default_port; varServer=NewMongo. Server (host,port,{auto_reconnect:true});//Server server where the database is created vardb=NewMongo. Db ("Node-mongo-examples", Server,{safe:true});//To Create a database objectDb.open (function(ERR,DB) {//connecting to a database if(ERR)Throwerr; Else{db.collection ("Users",function(err,collection) {Collection.insert ({username:"Hope", FirstName: "Li"},function(Err,docs) {console.log (docs); Db.close (); }); }); } }); Db.on ("Close",function(ERR,DB) {//Close the database if(ERR)Throwerr; ElseConsole.log ("Successfully closed database.")); });
Close Database db.close ([Forceclose],[callback]);
When Forceclose is true, the database is forced to close, and when the database is closed, open can no longer be used to open the database.
When Forceclose is false, the database is not forced to close, and when the database is closed, it can be opened again using open.
When Foreclose is true:
varMongo=require ("MongoDB"); varhost= "localhost"; varport=MONGO. Connection.default_port; varServer=NewMongo. Server (host,port,{auto_reconnect:true});//Server server where the database is created vardb=NewMongo. Db ("Node-mongo-examples", Server,{safe:true});//To Create a database objectDb.open (function(ERR,DB) {//connecting to a database if(ERR)Throwerr; Else{db.collection ("Users",function(err,collection) {Collection.insert ({username:"Hope", FirstName: "Li"},function(Err,docs) {console.log (docs); Db.close (false); }); }); } }); Db.once ("Close",function(ERR,DB) {//Close the database if(ERR)Throwerr; Else{Db.open (function(err,db) {db.collection ("Users",function(err,collection) {Collection.insert ({username:"Three", FirstName: "Zhang"},function(err,docs) {if(ERR)Throwerr; Else{console.log (docs); Db.close (true); } }) }); }); } });
reading Data
varMongo=require ("MongoDB");varhost= "localhost";varport=MONGO. Connection.default_port;varServer=mongo. Server (host,port,{auto_reconnect:true});vardb=NewMongo. Db ("Node-mongo-examples", Server,{safe:true});d B.open (function(err,db) {db.collection ("Users",function(err,collection) {if(ERR)Throwerr; Else{collection.find ({}). ToArray (function(err,docs) {if(ERR)Throwerr; Else{console.log (docs); Db.close (); } }); } });});
Search with query criteria
varMongo=require ("MongoDB");varhost= "localhost";varport=MONGO. Connection.default_port;varServer=mongo. Server (host,port,{auto_reconnect:true});vardb=NewMongo. Db ("Node-mongo-examples", Server,{safe:true});d B.open (function(err,db) {db.collection ("Users",function(err,collection) {if(ERR)Throwerr; Else{collection.find ({username:{$inch: ["Yan Si", "Three"]}}). ToArray (function(err,docs) {if(ERR)Throwerr; Else{console.log (docs); Db.close (); } }); } });});
insert a batch of data and search Type==food and the Price field value is less than 10
varMongo=require ("MongoDB");varhost= "localhost";varport=MONGO. Connection.default_port;varServer=mongo. Server (host,port,{auto_reconnect:true});vardb=NewMongo. Db ("Node-mongo-examples", Server,{safe:true});vardocs=[{type:"Food", price:11}, {type:"Food", price:10}, {type:"Food", price:9}, {type:"Food", price:8}, {type:"Book", Price:9}];d B.open (function(err,db) {db.collection ("Goods",function(err,collection) {if(ERR)Throwerr; Else{collection.insert (docs,function(err,docs) {if(ERR)Throwerr; Else{collection.find ({type:"Food", price:{$lt: 10}}). ToArray (function(err,docs) {if(ERR)Throwerr; Else{console.log (docs); Db.close (); } } ); } }) } });});
The expression in a query :
Collection.find ({$or: [{type:' food '}, {price:{$lt:Ten}}]})
node. JS manipulating the MongoDB database