node. JS manipulating the MongoDB database

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.