gpt4 book ai didi

MongoDB 触发引用更新

转载 作者:可可西里 更新时间:2023-11-01 10:42:41 26 4
gpt4 key购买 nike

我在 mongodb 中有以下示例用户集合:

{
_id: '1234',
name: 'abc',
age: '20',
...
}

还使用示例数据对集合进行排序:

{
_id: '6789',
total: '300',
cutomer: {
_id: '1234',
name: 'abc'
},
...
}

在我的用户页面上,用户更新了他的名字。如何触发对订单集合中使用的所有用户引用的自动更新。

最佳答案

像这样,您必须查询该用户 ID,然后使用选项 multi: true 更新所有记录:

orders.update({customer: {_id: 1234}}, {$set: {customer: {name: 'newname'}}}, {multi: true}, function (err, affectedRows) {
// Do something
});

关于MongoDB 触发引用更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34115651/

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