promified MongoDB Node driver .forEach() return 做了什么样的promise?

What kind of promise does the promisified MongoDB Node driver .forEach() return?

我最近开始在我的 Node.js 项目中使用箭头函数、promises (bluebird) 和 MongoDB。正如您在下面看到的,我承诺了所有 mongodb 本机驱动程序。到目前为止,就像一个魅力。

现在,我想使用.forEach() 方法,我想知道我应该如何使用它。这是想法:

var Promise = require("bluebird");
var mongodb = Promise.promisifyAll(require("mongodb"));
var MongoClient = mongodb.MongoClient;

MongoClient
.connect('the_URL_of_my_database')
.then( (database) => {
  var db = database;
  var myCollection = database.collection('my_collection');

  myCollection
  .find('my_awesome_selector')
  .forEach( (document) => {
    
    var selector = {hashkey: document.hashkey};
    var update = {$set: {data: 'my_new_data'}};
    
    myCollection
    .updateOne(selector, update)
    .then( (response) => {
      // do something if needed
    })
    .catch( (err) => {/*update error handling*/});
    
  })
  .then( (/*do we get some kind of input here ?*/) => {
    // do something once EVERY document has been updated
  })
  .catch( (err) => {/*forEach error handling*/});

})
.catch( (err) => {/*connection error handling*/});

我的问题是:

  1. 承诺的 .forEach() return 是什么?是否承诺会在所有文档更新后得到解决?
  2. 如果我的代码不起作用,是否知道如何正确实施它?

[对我有很大帮助的可选问题:]

  1. 我注意到 MongoDB 驱动程序中的 .each() 方法。它被标记为 "deprecated",但它有什么用处,还是我应该坚持使用 .forEach()?
  2. 整个片段对您来说是否有意义,或者有什么菜鸟 mistakes/improvements 我应该注意的吗?

非常感谢任何回答或建议!

[编辑:我精确更新了。我觉得我又一次陷入了回调地狱]

你不需要蓝鸟来做这些。 mongo 驱动程序内置了 promise。由于 forEach 没有 return 任何东西,您可以使用 map() 将每个文档更新处理为一个承诺,使用 toArray() 将它们缩减为一个承诺数组,然后Promise.all() 解决该数组中的每个承诺:

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

MongoClient
  .connect('the_URL_of_my_database')
  .then((db) => {

    var myCollection = db.collection('my_collection');

    myCollection.find({})
      .map((document) => {

        var selector = { _id: document._id };
        var update = { $set: { data: 'my_new_data' } };
        return myCollection
          .updateOne(selector, update)
          .then((response) => {
            // this is each resolved updateOne. You don't need
            // this `.then` unless you want to post-process
            // each individual resolved updateOne result
            console.log(response.result);
            // return each value to accumulate it in Promise.all.then()
            return response.result;
          });
      })
      .toArray() // accumulate the .map returned promises
      .then((promises) => Promise.all(promises)) // iterate through and resolve each updateOne promise
      .then((resolvedPromises) => {
        // an array of all the results returned from each updateOne
        console.log(resolvedPromises);
      })
      .catch((error) => {
        // error handle the entire chain.
      });
  })
  .catch((error) => {
    // error handle the connection
  });

I have noticed the .each() method in the MongoDB driver. It is marked as "deprecated", but is there any use for it, or should I stick to .forEach() ?

为了与 Array 原型保持一致,我认为 .each() 已被弃用,取而代之的是 .forEach()