node. JS Query Statement Encapsulation

Source: Internet
Author: User
Tags mongoclient

/**
* Created by Administrator on 2017/3/3 0003.
*/
var mongo=require (' MongoDB ');

var config=require ("./config.js"). Config;

var mongoclient=mongo.mongoclient;

var Objectid=mongo.objectid;

/* Connect to Database */
function _connectdb (callback) {
Mongoclient.connect (Config.dburl,function (err,db) {
if (err) {
Console.log (' Database connection failed ... ');
Callback (err, NULL);
Return
// }
Callback (err, DB);
//})

Mongoclient.connect (Config.dburl,function (err,db) {

if (err) {
Console.log (' Database connection failed ... ');
Callback (err, NULL);
Return
}
Callback (err, DB);
})

}





Used to get ID
Exports. Objectid=objectid;

Exports.count=function (Collectionname,json,callback) {


_connectdb (function (err,db) {
Db.collection (CollectionName). Count (JSON, function (err, result) {
Callback (err, result);
Db.close (); Close the database
})
})

}

/* Insert Data */
Exports.insertone=function (Collectionname,json,callback) {


_connectdb (function (err,db) {
Db.collection (CollectionName). Insertone (JSON, function (err, result) {
Callback (err, result);
Db.close (); Close the database
})
})

}
/* Update Data */


Modify a Bar
Exports.updateone = function (CollectionName, Json1, Json2, callback) {
_connectdb (function (err, db) {
Db.collection (CollectionName). Updateone (
Json1,
Json2,
function (err, results) {
Callback (err, results);
Db.close ();//Close Database
});
})
}
Modify All
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 Database
});
})


}


Find

Requires a minimum of 4 parameters
Db.find (' table name ', {},{},function () {
//
//})

CollectionName Table Name

Json1 Query criteria
Columns returned by Json2
C Configuration Callback
D Callback

Exports.find=function (CollectionName, Json1,json2, C, D) {

if (arguments.length<4) {

Console.log (' Incoming parameter is incorrect ');
return false;
}

var result = []; Result array
if (arguments.length = = 4) {
Then the parameter C is callback, and parameter d is not passed.
var callback = C;
var skipnumber = 0;
Number limit
var limit = 0;
} else if (arguments.length = = 5) {
var callback = D;
var args = C; /* Configuration */
Number of bars that should be omitted 1
var skipnumber = args.pagesize * (args.page-1) | | 0;

Page=1 first page: Skip (0)
page=2 first page: Skip (2)
Page=3 first page: Skip (4)
Page=4 first page: Skip (6)

Number limit
var limit = Args.pagesize | | 20;
Sorting method
var sort = Args.sort | | {};
} else {
throw new Error ("The number of arguments to the Find function must be 3, or 4.") ");
Return
}


_connectdb (function (err,db) {


if (err) {/* database connection Failed */

Console.log (' Database connection failed ');
Return
}


var userrel=db.collection (CollectionName). Find (JSON);
var userrel=db.collection (CollectionName). Find (Json1,json2). Skip (Skipnumber). Limit. Sort (sort);


Userrel.each (function (err, doc) {

if (err) {
Res.write ("Cursor traversal error");
Return
}
if (doc! = null) {
Result.push (DOC);
} else {
Console.log (result);
Traverse complete
Db.close ();
Callback (Err,result)
}
});

})




}



//
Find
Exports.find=function (CollectionName, JSON, C, D) {
//
//
//
var result = []; Result array
if (arguments.length = = 3) {
Then the parameter C is callback, and parameter d is not passed.
var callback = C;
var skipnumber = 0;
Number limit
var limit = 0;
} else if (arguments.length = = 4) {
var callback = D;
var args = C; /* Configuration */
Number of bars that should be omitted 1
var skipnumber = args.pagesize * (args.page-1) | | 0;
//
//page=1 first page: Skip (0)
page=2 first page: Skip (2)
Page=3 first page: Skip (4)
Page=4 first page: Skip (6)
//
Number limit
var limit = Args.pagesize | | 20;
Sorting method
var sort = Args.sort | | {};
} else {
throw new Error ("The number of arguments to the Find function must be 3, or 4.") ");
Return
// }
//
//
_connectdb (function (err,db) {
//
//
if (err) {/* database connection Failed */
//
Console.log (' Database connection failed ');
Return
// }
//
//
var userrel=db.collection (CollectionName). Find (JSON);
var userrel=db.collection (CollectionName). Find (JSON). Skip (Skipnumber). Limit. Sort (sort);
//
//
Userrel.each (function (err, doc) {
//
if (err) {
Res.write ("Cursor traversal error");
Return
// }
if (doc! = null) {
Result.push (DOC);
} else {
Console.log (result);
Traverse complete
Db.close ();
Callback (Err,result)
// }
// });
//
// })
//
//
//
//
//}

Delete
Exports.deletemany = function (CollectionName, JSON, callback) {
_connectdb (function (err, db) {
Delete
Db.collection (CollectionName). Deletemany (
Json
function (err, results) {
Callback (err, results);
Db.close (); Close the database
}
);
});
}

node. JS Query Statement Encapsulation

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.