NodeJS:mono 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 });
        });
    });
}