📜  mongodb js insertmany - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:58.798000             🧑  作者: Mango

代码示例1
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/test';
var data1={
    name:'Data1',
    work:'student',
    No:4355453,
    Date_of_birth:new Date(1996,10,17)
};

var data2={
    name:'Data2',
    work:'student',
    No:4355453,
    Date_of_birth:new Date(1996,10,17)
};

MongoClient.connect(url, function(err, db) {
    if(err!=null){
        return console.log(err.message)
    }

    //insertOne
    db.collection("App").insertOne(data1,function (err,data) {

        if(err!=null){
            return console.log(err);
        }
        console.log(data.ops[0]);
    });

    //insertMany

var Data=[data1,data2];

db.collection("App").insertMany(Data,forceServerObjectId=true,function (err,data) {

        if(err!=null){
            return console.log(err);
        }
        console.log(data.ops);
    });
    db.close();
});