【问题标题】:MongoDB trigger reference updatesMongoDB 触发参考更新
【发布时间】:2016-03-10 23:57:08
【问题描述】:

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

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

还将样本数据的集合排序为:

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

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

【问题讨论】:

    标签: mongodb mongoose database


    【解决方案1】:

    使用类似的方法,您必须查询该用户 ID,然后使用选项 multi: true 更新所有记录:

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

    【讨论】:

    • 它对我不起作用。我收到WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })
    • 请用您的完整代码编辑您的问题(不在此处)。
    • 这些是我执行的查询db.customer.insert({ _id: '1234', name: 'abc', age: 21 })db.orders.insert({ _id: '6789', "total" : "300", "customer" : { "_id" : "1234", "name" : "abc" }})db.Collection.update({customer: {"_id" : '1234'}}, {$set: {cutomer: {name: 'newname'}}}, {multi: true})
    • 名称Collection只是一个例子,你必须使用你的集合名称db.orders.update,然后注意你写的cutomercustomer是不同的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多