简体   繁体   English

使用mongoose在MongoDB中批量插入

[英]Bulk insert in MongoDB using mongoose

I currently have a collection in Mongodb say "Collection1". 我目前在Mongodb有一个集合说“Collection1”。 I have the following array of objects that need to be into inserted into MongoDB. 我有以下需要插入MongoDB的对象数组。 I am using Mongoose API. 我正在使用Mongoose API。 For now, I am iterating through the array and inserting each of them into mongo. 现在,我正在迭代数组并将它们中的每一个插入到mongo中。 This is ok for now, but will be a problem when the data is too big. 现在这没问题,但是当数据太大时会出现问题。 I need a way of inserting the data in bulk into MongoDB without repetition. 我需要一种将数据批量插入MongoDB而无需重复的方法。 I am not sure how to do this. 我不知道该怎么做。 I could not find a bulk option in Mongoose. 我在Mongoose找不到批量选项。

My code below 我的代码如下

myData = [Obj1,Obj2,Obj3.......]

myData.forEach(function(ele){
      //console.log(ele)
     saveToMongo(ele);
    });
function saveToMongo(obj){
    (new Collection1(obj)).save(function (err, response) {
          if (err) {
             // console.log('Error while inserting: ' + obj.name + " " +err);
          } else {
            // console.log('Data successfully inserted');
          }
      });

      return Collection1(obj);
  }

You might want to use the insertMany() method here if you're using the latest Mongoose version 4.4.X and greater, which essentially uses Model.collection.insertMany() under the hood and the driver might handle parallelizing >= 1000 docs for you. 如果你使用最新的Mongoose版本4.4.X和更高版本,你可能想在这里使用insertMany()方法,它本质上使用了引擎盖下的Model.collection.insertMany() ,驱动程序可能会处理并行化>= 1000文档您。

myData = [Obj1, Obj2, Obj3.......];
Collection1.insertMany(myData, function(error, docs) {});

or using Promises for better error handling 或使用Promises更好地处理错误

Collection1.insertMany(myData)
    .then(function(docs) {
         // do something with docs
    })
    .catch(function(err) {
        // error handling here
    });

It works by creating a bunch of documents, calls .validate() on them in parallel, and then calls the underlying driver's insertMany() on the result of toObject({ virtuals: false }); 它的工作原理创建了一堆文件,调用.validate()在平行于它们,然后调用底层驱动程序的insertMany()上的结果toObject({ virtuals: false }); of each doc. 每个文件。 Although insertMany() doesn't trigger pre-save hooks, it has better performance because it only makes 1 round-trip to the server rather than 1 for each document. 虽然insertMany()不会触发预保存挂钩,但它具有更好的性能,因为它只对服务器进行1次往返,而不是每个文档1次。


For Mongoose versions ~3.8.8, ~3.8.22, 4.x which support MongoDB Server >=2.6.x , you could use the Bulk API as follows 对于支持MongoDB Server >=2.6.x Mongoose版本~3.8.8, ~3.8.22, 4.x ,您可以使用Bulk API如下

var bulk = Collection1.collection.initializeOrderedBulkOp(),
    counter = 0;

myData.forEach(function(doc) {
    bulk.insert(doc);

    counter++;
    if (counter % 500 == 0) {
        bulk.execute(function(err, r) {
           // do something with the result
           bulk = Collection1.collection.initializeOrderedBulkOp();
           counter = 0;
        });
    }
});

// Catch any docs in the queue under or over the 500's
if (counter > 0) {
    bulk.execute(function(err,result) {
       // do something with the result here
    });
}

you can pass an array of objects to mongoose model create function 你可以将一组对象传递给mongoose模型创建函数

var Collection1 = mongoose.model('Collection1');

Collection1.create(myData,function(err){
    if(err) ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM