パッケージnodejsによるMongoDBの増量、削除、修正、調査


var mongo = require("mongodb").MongoClient;

var url = "mongodb://127.0.0.1:27017/test3"; 

function insertOne(collection, obj, callBack){ //    
    mongo.connect(url, function (error, db) {
        if (error == null) {
            var database = db.db("test3");
            database.collection(collection).insertOne(obj, callBack)
        } else {
            console.log(error)
        }
        db.close();
    })
}

function insertMany(collection, objArr, callBack){ //    
    mongo.connect(url, function (error, db) {
        if (error == null) {
            var database = db.db("test3");
            database.collection(collection).insertMany(objArr, callBack)
        } else {
            console.log(error)
        }
        db.close();
    })
}

function find(collection, where, callBack){  //   
    mongo.connect(url, function (error, db) {
        if (error === null) {
            var database = db.db("test3");
            database.collection(collection).find(where).toArray(callBack);
        } else {
            console.log(error)
        }
        db.close();
    })
}

function update(collection, where, update, callBack) { //   
    mongo.connect(url, function (error, db) {
        if (error === null) {
            var database = db.db("test3");
            database.collection(collection).updateOne(where, update, callBack);
        } else {
            console.log(error)
        }
        db.close();
    })
}

function deleteData(collection, where, callBack) { //   
    mongo.connect(url, function (error, db) {
        if (error === null) {
            var database = db.db("test3");
            database.collection(collection).deleteOne(where, callBack);
        } else {
            console.log(error)
        }
        db.close();
    })
}


module.exports = {
    "insertOne": insertOne,
    "insertMany": insertMany,
    "update": update,
    "find": find,
    "deleteData": deleteData
}