[英]Mongoose - findOneAndUpdate dynamically $push into array
[英]Mongoose findOneAndUpdate on array of subdocuments
我正在嘗試用該數組的新副本替換子文檔數組。
就像是...
var products = productUrlsData; //new array of documents
var srid = the_correct_id;
StoreRequest.findOneAndUpdate({_id: srid}, {$set: {products: products}}, {returnNewDocument : true}).then(function(sr) {
return res.json({ sr: sr}); //is not modified
}).catch(function(err) {
return res.json({err: err});
})
產品var具有正確的修改,但是返回的對象以及db中的文檔均未修改。 這不是替換子文檔數組的字段的正確方法嗎? 如果沒有,那是什么?
我參加聚會有點晚了,而且我真的很着急-但應該是:
StoreRequest.updateOne(
{ _id: srid },
{ $set: { 'products.$': products }},
{ new: true });
我無法使其與findOneAndUpdate
一起findOneAndUpdate
但以上方法確實可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.