简体   繁体   English

使用nodejs mongodb驱动程序和async.js在mongodb中插入多个记录

[英]Insert multiple records in mongodb with nodejs mongodb driver and async.js

Currently I am facing a callback hell while trying to execute a number of entries into mongodb with nodejs driver. 目前,我试图通过nodejs驱动程序在mongodb中执行许多条目时遇到回调地狱。 There are certain tasks that should be performed only if all the records (in array) are inserted. 只有插入了所有记录(在数组中),才应执行某些任务。 I checked out the async.each method but the main issue with this is that it will invoke the callback if an error is encountered. 我签出了async.each方法,但是主要的问题是,如果遇到错误,它将调用回调。 Hence the rest of the items in collection won't be processed. 因此,集合中的其余项目将不被处理。

So what is the correct way to deal with this kind of problem with async.js? 那么,使用async.js处理此类问题的正确方法是什么?

PS: Please an example would be very helpful as I am just starting out with async.js and there are very less(if at all) examples to use this for batch inserts into mongodb using the api. PS:请举一个示例,这对我非常有用,因为我只是从async.js开始,并且很少(如果有的话)示例可用于使用api将其批量插入mongodb。

Let's describe the problem. 让我们描述问题。

  1. You have an iteration based flow with some library (example.: AsyncJS) 您具有一些库的基于迭代的流程(示例:AsyncJS)
  2. Async Flow can be interrupted at some iteration 异步流可能会在某些迭代中中断
  3. You want to be sure all the operatiosn in MongoDB are correctly stored or maybe take a future decision if that operation should happen or no 您想确保MongoDB中的所有操作均已正确存储,或者将来决定是否执行该操作

In this case i can suggest you to use Bulk Operation 在这种情况下,我可以建议您使用批量操作

 // array of operations
 let batch = yourCollection.initializeUnorderedBulkOp();

 async.each (yourArrayToIterate, function (item, next) {
     // add some Operation that will be execute lated
     batch.insert({firstName:"Daniele"});
     batch.find({firstName: "Daniele"}).updateOne({$set: {lastName:"Tassone"}});
     // other code to do...some Old Library
       try {
         someOldLibrary.doSomethingThatCanCreateAnExpection();
      } catch (e) {
         //some error happen
      }
     // more modern approach with New Library
      someNewLibrary
       .then(function (result){
       })
       .catch(function (err) {
           // some error happen
       }

 }, function (err, result) {

      // Flow is now completed, take a decision
     if (err) {
       // do nothing, maybe?
     } else {
        // execute all the Ordered Bulk 
        batch.execute().then(function(executionResult) {
            // done, you can 
        });
     }
 });

Some Note: 1) BulkOperation can be Ordered and UnOrdered. 注意:1)BulkOperation可以有序和无序。 UnOrdered perform better and continue to work even if some error happen 2) Ordered is performed sequentially and if some error happen MongoD will not continue to the next element in the Bulk. 无序执行性能更好,即使发生某些错误也可以继续工作2)有序执行是顺序执行的,即使发生某些错误,MongoD也不会继续执行到Bulk中的下一个元素。

Documentation: http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#initializeOrderedBulkOp 文档: http : //mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#initializeOrderedBulkOp

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

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