node to MONGODB implementation of additions and deletions, paging function

Source: Internet
Author: User
Tags mongoclient

An increase in the content of the previous article:

node. JS Environment for MONGODB implementation Check, plug, paging function

It was originally written, but then there is a bug, do not know where the problem, yesterday only found Updatemany function to pass the argument when the CollectionName, idiot a day. Eh =-=

//Limit,skip Efficient PracticesvarExpress = Require ("Express");varApp =Express ();vardb = require ("./model/db3.js"); App.get ("/",function(req,res) {Db.insertone ("Teacher", {"name": "Little Red"},function(err,result) {if(Err) {Console.log ("Insert Failed"); } res.send ("Insert Succeeded"); });}); App.get ("/du",function(req, res) {//This page now accepts a page parameter.     varpage = parseint (req.query.page);//It's easy to read the get parameters in Express    //Find 4 parameters, in which set to check, what to check, paging setup, after checking what to do    //10 per page, check the third page    //db.find ("student", {},{"Pageamount": 5, "page":p age},function (err,result) {    //db.find ("student", {},function (err,result) {    //Note that this result is still 5, is the first find in the result set of age greater than 50, skip and limitDb.find ("Student", {"age": {$gt: 50}},{' Pageamount ": 5," page ":p Age},function(err,result) {if(Err) {Console.log (err);    } res.send (Result); });}); App.get ("/shan",function(req,res) {//note Here is the string    varrestaurant_id =req.query.id; Db.deletemany ("Teacher", {"name": "Little Red"},function(err,result) {if(Err) {Console.log (err);    } res.send (Result); });}); App.get ("/xiugai",function(req,res) {//note Here is the stringDb.updatemany ("Student", {name:haha}, {$set: {name:Hee Hee}        },        function(err,result) {if(Err) {Console.log (err); Db.close ();//Close the database} res.send (Result); });}); App.listen (3000);
//after optimization, add the Skip,limit parameter to the wording//This module encapsulates all the common operations on the database .varSettings = require (".. /settings.js ");varMongoclient = require (' MongoDB '). mongoclient;//regardless of the database operation, are connected to the database first, so we can connect the database//encapsulation becomes functionfunction_connectdb (callback) {varURL =Settings.dburl; Mongoclient.connect (URL,function(err,db) {if(Err) {callback (Err,NULL); return;    } callback (ERR,DB); });} Exports.insertone=function(CollectionName, JSON, callback) {_connectdb (function(err,db) {if(Err) {callback (Err,NULL); return; } db.collection (CollectionName). Insertone (JSON,function(err, result) {callback (err, result); Db.close ();//Close the database            })        });} Exports.find=function(collectionname,json,c,d) {varresult = [];//result Array    //JS has no function overloads, only manual implementation    if(Arguments.length = = 3) {       //if the args are not passed        //then the parameter C is callback, and parameter d is not passed.         varcallback =C; varSkipnumber = 0; //number limit, limit (0) is no limit        varLimit = 0; }Else if(Arguments.length = = 4) {        varargs =C; varcallback =D; //number of bars that should be omitted        varSkipnumber = Args.pageamount *Args.page; //Number Limit        varLimit =Args.pageamount; }Else {        Throw NewError ("The number of arguments to the Find function must be 3, or 4"); return; }    //start from page 0thConsole.log ("Skip" +skipnumber+ "bar" + "limit to" +limit+ "bar"); //link Database, link to find all_connectdb (function(err,db) {varcursor =db.collection (CollectionName). Find (JSON). Skip (Skipnumber). Limit; Cursor.each (function(err, doc) {if(Err) {callback (Err,NULL);                Db.close (); return; }           if(Doc! =NULL) {Result.push (doc);//put into the result array}Else {               //Traverse End, no more documentsCallbackNULL, result); Db.close ();//Close the database           }        }); });} Exports.deletemany=function(collectionname,json,callback) {_connectdb (function(err, db) {//Deletedb.collection (CollectionName). Deletemany (JSON,function(err, results) {callback (err, results);            Db.close ();    }        ); });} Exports.updatemany=function(collectionname,json1,json2,callback) {_connectdb (function(err,db) {db.collection (CollectionName). Updatemany (Json1, Json2,function(Err,results) {callback (err,results); Db.close ();//Close the database            }); });};

node to MONGODB implementation of additions and deletions, paging function

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.