Nodejs Operation MongoDB Database (MONGODB)

Source: Internet
Author: User
Tags mongoclient

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)

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.