Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用修复程序更新:是否有一种方法可以在每次迭代中循环对象并保存/更新mongodb文档,而不会出现ParallelSaveError?_Mongodb_Loops_Object_Mongoose - Fatal编程技术网

使用修复程序更新:是否有一种方法可以在每次迭代中循环对象并保存/更新mongodb文档,而不会出现ParallelSaveError?

使用修复程序更新:是否有一种方法可以在每次迭代中循环对象并保存/更新mongodb文档,而不会出现ParallelSaveError?,mongodb,loops,object,mongoose,Mongodb,Loops,Object,Mongoose,我想在每个循环中更新mongodb文档,但似乎不起作用。我用map在对象中循环,得到了一个 parallelSaveError:无法并行多次保存()同一文档 因此,现在使用async.each循环对象,但mongoose似乎只保存一次。代码如下 const openTrade = await TradeModel.find({ $and: [{ isOpen: true }, { price: { $lte: price } }] }) .sort("volume") .where(

我想在每个循环中更新mongodb文档,但似乎不起作用。我用map在对象中循环,得到了一个

parallelSaveError:无法并行多次保存()同一文档

因此,现在使用async.each循环对象,但mongoose似乎只保存一次。代码如下

const openTrade = await TradeModel.find({
  $and: [{ isOpen: true }, { price: { $lte: price } }]
})
  .sort("volume")
  .where({ userId: { $ne: req.authUser._id } });
trade = async openTrade => {
  const buyerInfo = await UserModel.findById(req.authUser._id);
  const buyerWallet = await WalletModel.findById(buyerInfo.walletId);
  const sellerInfo = await UserModel.findById(openTrade.userId);
  const sellerWallet = await WalletModel.findById(sellerInfo.walletId);

  buyerWallet.balance = parseInt(buyerWallet.balance) - amountToPay;
  sellerWallet.balance = parseInt(sellerWallet.balance) + amountToPay;
  await Promise.all([sellerWallet.save(), buyerWallet.save()]);
};
async.forEachOf(openTrade, trade);

所以我终于找到了问题所在。我没有将buyerInfo、sellerInfo和Wallets变量设置为常量。所以现在它起作用了。谢谢

您可以尝试以下操作:根据MongoDB,如果文档包含一个_id字段,那么save()方法相当于更新,upsert选项设置为true,并且_id字段上的查询谓词

const openTrade = await TradeModel.find({$and:[{isOpen : true}, {price : {$lte : price}}]}).sort('volume').where({userId : {$ne : req.authUser._id } })
            trade = async (openTrade)=>{
            buyerInfo = await UserModel.findById(req.authUser._id)
            buyerWallet = await WalletModel.findById(buyerInfo.walletId)
            sellerInfo = await UserModel.findById(openTrade.userId)
            sellerWallet = await WalletModel.findById(sellerInfo.walletId)
            delete buyerWallet._id;
            delete sellerWallet._id;
            buyerWallet.balance = parseInt(buyerWallet.balance) - amountToPay
            sellerWallet.balance = parseInt(sellerWallet.balance) + amountToPay  
           await Promise.all([sellerWallet.save(), buyerWallet.save()])

        }
        async.forEachOf(openTrade, trade)

尝试从sellerWallet和buyerWallet对象中删除“_id”。如果文档包含_id字段,则保存相当于更新,那么为什么要删除_id。此外,我注意到删除buyerWallet。_id在转换为toObject()之前不起作用,toObject()之后不会更新文档。