Correct way to insert many records into Mongodb with Node.js

前端 未结 4 1165
暗喜
暗喜 2020-12-09 10:06

I was wondering what is the correct way to do bulk inserts into Mongodb (although could be any other database) with Node.js

I have written the following code as an e

4条回答
  •  时光取名叫无心
    2020-12-09 10:40

    New in version 3.2.

    The db.collection.bulkWrite() method provides the ability to perform bulk insert, update, and remove operations. MongoDB also supports bulk insert through the db.collection.insertMany().

    In bulkWrite it is supporting only insertOne, updateOne, updateMany, replaceOne, deleteOne, deleteMany

    In your case to insert data using single line of code, it can use insertMany option.

     MongoClient.connect('mongodb://127.0.0.1:27017/test', function (err, db) {
                var i, collection;
                if (err) {
                    throw err;
                }
                collection = db.collection('entries');
                collection.insertMany(entries)
                db.close();
            });

提交回复
热议问题