NodeJS:mongo db 入门级使用CURD封装

Mongo版本:4.2.*

nodejs版本:12.x以上

var MongoClient = require('mongodb').MongoClient;
var log4js = require('log4js');
log4js.configure('./config/log4js.json');
var logger = log4js.getLogger();
var log = require('./log.js');
var moment = require('moment');
var Obj = require('./object.js');
var config_db = require('./config.db.js');

var db_url = config_db.mongo.db_url;
// var db_url = db_url + '/xxxxx';
var db_name = 'xxxxx'
exports.insertPatch = insertPatch;
/**
 * 批量添加
 * @param {*} collection 集合
 * @param {*} values 批量添加的数据
 */
function insertPatch(collection, values) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).insertMany(values, function(err, res) {
            if (err) throw err;
            db.close();
        });
    });
}
exports.findOne = findOne;

function findOne(db_url, db_name, collection, where, fields, user, password, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).findOne(where, fields, function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            db.close();
            next(err, result);
        });
    });
}

exports.insertOne = insertOne;
/**
 *添加一条记录
 *
 * @param {*} collection
 * @param {*} value
 */
function insertOne(collection, value, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).insertOne(value, function(err, res) {
            if (err) throw err;
            // res.insertedCount
            next({ "err": err, "res": res });
        });
    });
}

exports.selectFirstOne = selectFirstOne;
/**
 *查询数据,如果返回为{}则无数据
 *
 * @param {*} collection
 * @param {*} where
 */
function selectFirstOne(collection, where) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).find(where).toArray(function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            logger.info("-----------=========-----------");
            logger.info(result);
            db.close();
            if (result.length > 0) {
                return result[0];
            } else {
                return {};
            }
        });
    });
}

exports.selectWhere = selectWhere;
/**
 * 
 * @param {*} db_url 
 * @param {*} db_name 
 * @param {*} collection 
 * @param {*} where 查询条件
 * @param {*} fields 想要查询的具体字段
 * @param {*} user 
 * @param {*} password 
 * @param {*} next 
 */
function selectWhere(db_url, db_name, collection, where, fields, user, password, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).find(where, fields).toArray(function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            db.close();
            next(err, result);
        });
    });
}

exports.selectWhereSort = selectWhereSort;
/**
 * 
 * @param {*} db_url 
 * @param {*} db_name 
 * @param {*} collection 
 * @param {*} where 查询条件
 * @param {*} fields 想要查询的具体字段
 * @param {*} sort 字典{'macAddress':-1} 
 * @param {*} user 
 * @param {*} password 
 * @param {*} next 
 */
function selectWhereSort(db_url, db_name, collection, where, fields, sort, user, password, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).find(where, fields).sort(sort).toArray(function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            db.close();
            next(err, result);
        });
    });
}

exports.selectWhereOrderLimit = selectWhereOrderLimit;
/**
 * 
 * @param {*} db_url 
 * @param {*} db_name 
 * @param {*} collection 
 * @param {*} where 查询条件
 * @param {*} fields 想要查询的具体字段
 * @param {*} user 
 * @param {*} password 
 * @param {*} next 
 */
function selectWhereOrderLimit(db_url, db_name, collection, where, fields, sort, limit, user, password, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        // sort = {'account_order':1};
        // limit = 10;分页用
        console.log(sort, limit);
        dbo.collection(collection).find(where, fields).sort(sort).limit(limit).toArray(function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            db.close();
            next(err, result);
        });
    });
}

exports.selectWhereOrderSkipLimit = selectWhereOrderSkipLimit;
/**
 * 
 * @param {*} db_url 
 * @param {*} db_name 
 * @param {*} collection 
 * @param {*} where 查询条件
 * @param {*} fields 想要查询的具体字段
 * @param {*} user 
 * @param {*} password 
 * @param {*} next 
 */
function selectWhereOrderSkipLimit(db_url, db_name, collection, where, fields, sort, limit, skip, user, password, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        // sort = {'account_order':1};
        // limit = 10;分页用
        dbo.collection(collection).find(where, fields).sort(sort).skip(skip).limit(limit).toArray(function(err, result) { // 返回集合中所有数据
            if (err) throw err;
            db.close();
            next(err, result);
        });
    });
}

exports.updateOne = updateOne;
/**
 * 更新某一条记录
 * @param {} collection 
 * @param {*} where 
 * @param {*} values 
 */
function updateOne(collection, where, values, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        var updateStr = { $set: values };
        console.log(db_url);
        console.log(where);
        console.log(updateStr);
        dbo.collection(collection).updateOne(where, updateStr, function(err, res) {
            if (err) throw err;
            // res.modifiedCount
            db.close();
            next({ "err": err, "res": res });
        });
    });
}

exports.updatePatch = updatePatch;
/**
 * 更新某一条记录
 * @param {} collection 
 * @param {*} where 
 * @param {*} values 
 */
function updatePatch(collection, where, values, next) {
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        var updateStr = { $set: values };
        console.log(db_url);
        console.log(where);
        console.log(updateStr);
        dbo.collection(collection).updateMany(where, updateStr, function(err, res) {
            if (err) throw err;
            // res.modifiedCount
            // res.modifiedCount
            db.close();
            next({ "err": err, "res": res });
        });
    });
}


exports.updateOneBy_id = updateOneBy_id;
/**
 * 更新某一条记录通过 _id
 * @param {} collection 
 * @param {*} where 
 * @param {*} values 
 */
function updateOneBy_id(collection, singleResult, next) {
    let _id = singleResult._id;
    delete singleResult._id;
    MongoClient.connect(db_url, { useNewUrlParser: true }, function(err, db) {
        if (err) throw err;
        var dbo = db.db(db_name);
        dbo.collection(collection).updateOne({ "_id": _id }, { $set: singleResult }, function(err, res) {
            if (err) throw err;
            db.close();
            next({ "err": err, "res": res });
        });
    });
}

 

你可能感兴趣的:(nodejs,mongo,nodejs,mongo,curd,insert,select)