1. Increase
var mongoclient =Require"MongoDB"). Mongoclient;var Db_url ="Mongodb://localhost:27017/chm";functionInsertData(DB) {var devices = Db.collection (' VIP ');var data = { "name": "node", "age": 22, "addr": "NB", "Addtime": new date ()}; Devices.insert (Data,function ( Error, result) {if (Error) {Console.log (else{Console.log (RESULT.RESULT.N);} db.close ();}); Mongoclient.connect (Db_url, function ' connected successfully! '); InsertData (db);});
2. Find
var MongoDB =Require' MongoDB ')var mongoclient =Require' MongoDB '). Mongoclient;var db_conn_str =' Mongodb://localhost:27017/chm ';var selectdata =function(DB, Callback) {Connect to Tablevar collection = Db.collection (' VIP ');Querying datavar wherestr = { "name": ' node '}; Collection.find (Wherestr, function (error, cursor) { Cursor.each (function (Error, Doc) {if (doc) {//console.log (DOC); if (doc.addtime) {console.log (function (err, db) {Console.log (" Connection succeeded!) "); Selectdata (db, function (Result {Console.log (result); Db.close ();});
3. Update
var mongoclient =Require"MongoDB"). Mongoclient;var Db_url ="Mongodb://localhost:27017/chm"; Mongoclient.connect (Db_url,function(Error, DB) {Console.log ("Connection succeeded!"); UpdateData (db);});function updatedata(db) { var devices = db.collection (' VIP '); var wheredata = {"name":"Node"} var updatedat = {$set: {"age":26}}; //If not $set, replace the entire data devices.update (Wheredata, Updatedat, function(error, result) { if (error) { Console.log (' ERROR: ' + error);} else{Console.log (result);} db.close ();});
4. Delete
var mongoclient =Require' MongoDB '). Mongoclient;var Db_url ="Mongodb://localhost:27017/chm"; Mongoclient.connect (Db_url,function(error, DB) {Console.log ("Connection succeeded"); DeleteData (db);}); function deleteData(db) { var devices = db.collection (' VIP '); var data = {"name":"Node"}; devices.remove (data, function(error, result) { if (error) { Console.log (' ERROR: ' + error);} else{Console.log (RESULT.RESULT.N);} db.close ();})}
Stored Procedures
Create a stored procedure in MongoDB
All stored procedures reside in Db.system.js, through Db.eval ("Stored procedure ID ()"), and call the stored procedure.
Called in the Code:
var mongoclient =Require "MongoDB"). Mongoclient; var db_url = "Mongodb://localhost:27017/chm"; Mongoclient.connect (Db_url, function (error,db) {console.log ( "Connection succeeded!"); callprocess (DB)}); function callprocess (db) {Db. Eval ( "Get_vip_count ()", Functionif (Error) {Console.log (error);} else{console.log ( "Count:" +result);} Db.close (); });}
Nodejs Operation MongoDB Database (MongoDB)