如何并行执行更新

时间:2018-04-23 05:45:49

标签: javascript node.js mongodb mongoose

我注意到当我使用new Promise(async (resolve, reject)new Promise((resolve, reject))相比时,它正在执行查询,我在我的Promise.all([]).then块上得到了我想要的结果,如:

      const borrowersFeePromise = new Promise(async (resolve, reject) => {
        if (borrowersFee !== undefined && borrowersFee !== '') {
          const borrowersFeeUpserted = await Fees.update(
            {
              personId: mongoose.Types.ObjectId(personId),
              category: 'borrowersFee',
            },
            {
              $set: {
                amount: borrowersFee,
              },
            },
            { upsert: true },
          );
          resolve({
            upsert: borrowersFeeUpserted,
            hasPassed: true,
          });
        } else {
          resolve({
            upsert: null,
            hasPassed: true,
          });
        }
      });

      const extendedFeePromise = new Promise(async (resolve, reject) => {
        if (extendedFee !== undefined && extendedFee !== '') {
          const extendedFeeUpserted = await Fees.update(
            {
              personId: mongoose.Types.ObjectId(personId),
              category: 'extendedFee',
            },
            {
              $set: {
                amount: extendedFee,
              },
            },
            { upsert: true },
          );
          resolve({
            upsert: extendedFeeUpserted,
            hasPassed: true,
          });
        } else {
          resolve({
            upsert: null,
            hasPassed: true,
          });
        }
      });

      await Promise.all([
        borrowersFeePromise,
        extendedFeePromise,
      ]).then((feesResult) => {
        console.log(feesResult);

        /*
          outputs something like:
          [ { upsert: { n: 1, nModified: 1, ok: 1 }, hasPassed: true },
            { upsert: { n: 1, nModified: 1, ok: 1 }, hasPassed: true } ]
        */
      });   

但是,当我只使用new Promise((resolve, reject)时,它不执行查询并输出如下内容:

 [   { upsert:
    Query {
      _mongooseOptions: {},
      mongooseCollection: [Object],
      model: [Object],
      schema: [Object],
      op: 'update',
      options: [Object],
      _conditions: [Object],
      _fields: undefined,
      _update: [Object],
      _path: undefined,
      _distinct: undefined,
      _collection: [Object],
      _traceFunction: undefined,
      _count: [Function],
      _execUpdate: [Function],
      _find: [Function],
      _findOne: [Function],
      _findOneAndRemove: [Function],
      _findOneAndUpdate: [Function],
      _replaceOne: [Function],
      _updateMany: [Function],
      _updateOne: [Function] },
   hasPassed: true },
 ....]    

我认为promises是等待执行的,但为什么它只在我放async (resolve, reject)时执行?我打算在运行Promise.all的同时执行这两项费用,就像非阻塞命令一样。这是我在观看和重新学习有关它的教程后理解Promises的方式。我做错了吗?

非常感谢。

1 个答案:

答案 0 :(得分:2)

据我所知,这就是你真正想要的:

await Promise.all([
  Fees.update(
        {
          personId: mongoose.Types.ObjectId(personId),
          category: 'borrowersFee',
        },
        {
          $set: {
            amount: borrowersFee,
          },
        },
        { upsert: true },
  ),
  Fees.update(
        {
          personId: mongoose.Types.ObjectId(personId),
          category: 'extendedFee',
        },
        {
          $set: {
            amount: extendedFee,
          },
        },
        { upsert: true },
  )
])

但实际上,你应该这样做:

let results = await Fees.bulkWrite([
  { "updateOne": { 
    "filter": {
      personId: mongoose.Types.ObjectId(personId),
      category: 'borrowersFee',
    },
    "update": { $set: { amount: borrowersFee } },
    "options": { upsert: true },
 }},
 { "updateOne": { 
    "filter": {
      personId: mongoose.Types.ObjectId(personId),
      category: 'extendedFee',
    },
    "update": { $set: { amount: extendedFee } },
    "options": { upsert: true },
 }}
],{ "ordered": false })

使用bulkWrite()方法意味着“one”请求通过线路发送到服务器而不是通过执行单独的语句来解析“two”在平行下。因此,它们减少的开销不仅使其更快,而且{ "ordered": false }确保它们实际上在服务器上运行