Nodejs connects to the mongodb database for addition, deletion, modification, and query

Source: Internet
Author: User
Tags install mongodb mongoclient
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 ();});}

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.