【问题标题】:Mongoose findOneAndUpdate on array of subdocuments子文档数组上的猫鼬 findOneAndUpdate
【发布时间】:2019-04-08 23:34:05
【问题描述】:

我正在尝试用数组的新副本替换子文档数组。

类似...

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

products var 有正确的修改,但返回的对象以及 db 中的文档没有被修改。这不是替换子文档数组字段的正确方法吗?如果不是,那是什么?

【问题讨论】:

标签: javascript arrays node.js mongodb mongoose


【解决方案1】:

我参加聚会有点晚了,而且我真的很着急——但应该是:

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

我无法使其与 findOneAndUpdate 一起使用,但上述方法确实有效。

【讨论】:

    猜你喜欢
    • 2016-04-08
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 2020-11-18
    • 2021-09-25
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多