Nodejs connects to the mongodb database for addition, deletion, modification, and query
1. Install mongodb using the npm command
2. Install the mongodb Database
CRUD operation
We should have some knowledge about MongoDB before, and know some of its addition, deletion, query, modification, and command.
1. Add
Var login client = require ("mongodb "). using client; var DB_URL = "mongodb: // localhost: 27017/chm"; function insertData (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 ('error: '+ Error);} else {console. log (result. result. n);} db. close () ;}) ;}your client. connect (DB_URL, func Tion (error, db) {console. log ('Connection successful! '); InsertData (db );});
2. Search
Var mongodb = require ('mongodb ') var MongoClient = require ('mongodb '). using client; var DB_CONN_STR = 'mongodb: // localhost: 27017/chm'; var selectData = function (db, callback) {// connect to the table var collection = db. collection ('vip '); // query data var whereStr = {"name": 'node'}; collection. find (whereStr, function (error, cursor) {cursor. each (function (error, doc) {if (doc) {// console. log (doc); if (doc. addTime) {console. log ("AddTime:" + doc. addTime) ;}}) ;}) ;}}mongoclient. connect (DB_CONN_STR, function (err, db) {console. log ("connection successful! "); SelectData (db, function (result) {console. log (result); db. close ();});});
3. Update
Var login client = require ("mongodb "). using client; var DB_URL = "mongodb: // localhost: 27017/chm"; using client. connect (DB_URL, function (error, db) {console. log ("connection successful! "); UpdateData (db) ;}); function updateData (db) {var devices = db. collection ('vip '); var whereData = {"name": "node"} var updateDat ={$ set: {"age": 26 }}; // Replace the entire data devices if $ set is not used. update (whereData, updateDat, function (error, result) {if (error) {console. log ('error: '+ Error);} else {console. log (result);} db. close ();});}
4. Delete
Var login client = require ('mongodb '). using client; var DB_URL = "mongodb: // localhost: 27017/chm"; using client. connect (DB_URL, function (error, db) {console. log ("connection successful"); 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 Procedure
Create a stored procedure in mongodb
All stored procedures are stored in db. system. js, and stored procedures are called through db. eval ("Stored Procedure ID.
Call in code:
Var login client = require ("mongodb "). using client; var DB_URL = "mongodb: // localhost: 27017/chm"; using client. connect (DB_URL, function (error, db) {console. log ("connection successful! "); CallProcess (db)}); function callProcess (db) {db. eval ("get_vip_count ()", function (error, result) {if (error) {console. log (error);} else {console. log ("count:" + result);} db. close ();});}