gpt4 book ai didi

node.js - 使用 mongoose 在一个文档中进行多次更新

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:04 25 4
gpt4 key购买 nike

我想在一次 Mongoose 调用中更新单个文档中的多个值。这可能吗?

我有类似的东西:

update = {$inc : { numShown : 1 }, $inc : { secondField.subField : 1 }};
options = {};
MyModel.findByIdAndUpdate(req._id, update, options, function(err){
if(err){ return console.error(err);}
}

它运行,但不更新任何内容。

最佳答案

您需要将两个 $inc 值组合成一个对象并引用点键:

update = { $inc : { numShown : 1, 'secondField.subField' : 1 } };

关于node.js - 使用 mongoose 在一个文档中进行多次更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com