简体   繁体   English

在子文档数组上的猫鼬findOneAndUpdate

[英]Mongoose findOneAndUpdate on array of subdocuments

I'm trying to replace an array of sub-documents with a new copy of the array. 我正在尝试用该数组的新副本替换子文档数组。

Something like... 就像是...

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});
})

The products var has the correct modifications, but the returned object, as well as the document in the db, are not being modified. 产品var具有正确的修改,但是返回的对象以及db中的文档均未修改。 Is this not the correct way to replace a field which is an array of subdocuments? 这不是替换子文档数组的字段的正确方法吗? If not, what is? 如果没有,那是什么?

I am a bit late to the party plus I am really in a hurry -- but should be: 我参加聚会有点晚了,而且我真的很着急-但应该是:

StoreRequest.updateOne(
        { _id: srid },
        { $set: { 'products.$': products }},
        { new: true });

I couldn't make it work with findOneAndUpdate but the above does work. 我无法使其与findOneAndUpdate一起findOneAndUpdate但以上方法确实可以工作。

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

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