如何在嵌入式数组中更新后取回新值? [英] How to get back the new value after an update in a embedded array?

查看:11
本文介绍了如何在嵌入式数组中更新后取回新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下 MongoDB 集合:

Given the following MongoDB collection:

{
    "_id": ObjectId("56d6a7292c06e85687f44541"),
    "name": "My ranking list",
    "rankings": [
        {
            "_id": ObjectId("46d6a7292c06e85687f55542"),
            "name": "Ranking 1",
            "score": 1
        },
        {
            "_id": ObjectId("46d6a7292c06e85687f55543"),
            "name": "Ranking 2",
            "score": 10
        },
        {
            "_id": ObjectId("46d6a7292c06e85687f55544"),
            "name": "Ranking 3",
            "score": 15
        },
    ]
}

这是我如何提高给定排名的分数:

Here is how I increase the score of a given ranking:

db.collection.update(
    { "_id": ObjectId("56d6a7292c06e85687f44541"), "rankings._id" : ObjectId("46d6a7292c06e85687f55543") },
    { $inc : { "rankings.$.score" : 1 } }
);

我如何获得新的分数值?在上一个查询中,我将第二个排名从 10 增加到 11...更新后如何取回这个新值?

How do I get the new score value? In the previous query I increase the second ranking from 10 to 11... How do I get this new value back after the update?

推荐答案

如果您使用的是 MongoDB 3.0 或更新版本,则需要使用 .findOneAndUpdate() 并使用 projection 选项指定要返回的字段子集.您还需要将 returnNewDocument 设置为 true.当然你需要使用$elemMatch 这里的投影运算符,因为您不能使用位置投影并返回新文档.

If you are on MongoDB 3.0 or newer, you need to use the .findOneAndUpdate() and use projection option to specify the subset of fields to return. You also need to set returnNewDocument to true. Of course you need to use the $elemMatch projection operator here because you cannot use a positional projection and return the new document.

正如有人指出的那样:

您应该使用 .findOneAndUpdate() 因为 .findAndModify() 在每个官方语言驱动程序中都被突出显示为已弃用.另一件事是 .findOneAndUpdate() 的语法和选项在驱动程序之间非常一致.使用 .findAndModify(),大多数驱动程序不使用带有查询/更新/字段"键的同一个对象.因此,当有人申请另一种语言以保持一致时,就不会那么混乱了..findOneAndUpdate() 的标准化 API 更改实际上对应于服务器版本 3.x 而不是 3.2.x.完全不同的是,shell 方法在实现该方法时实际上落后于其他驱动程序(一次!).因此,大多数驱动程序实际上都有与具有此类更改的 3.x 版本相对应的重大版本更新.

You should be using .findOneAndUpdate() because .findAndModify() is highlighed as deprecated in every official language driver. The other thing is that the syntax and options are pretty consistent across drivers for .findOneAndUpdate(). With .findAndModify(), most drivers don't use the same single object with "query/update/fields" keys. So it's a bit less confusing when someone applies to another language to be consistent. Standardized API changes for .findOneAndUpdate() actually correspond to server release 3.x rather than 3.2.x. The full distinction being that the shell methods actually lagged behind the other drivers ( for once ! ) in implementing the method. So most drivers actually had a major release bump corresponding with the 3.x release with such changes.

db.collection.findOneAndUpdate( 
    { 
        "_id": ObjectId("56d6a7292c06e85687f44541"), 
         "rankings._id" : ObjectId("46d6a7292c06e85687f55543") 
    },  
    { $inc : { "rankings.$.score" : 1 } },  
    { 
        "projection": { 
            "rankings": { 
                "$elemMatch": { "_id" : ObjectId("46d6a7292c06e85687f55543") } 
            }
        }, 
        "returnNewDocument": true 
    }
)

从 MongoDB 3.0 开始,您需要使用 findAndModifyfields 选项还需要将 new 设置为 true 以返回新值.

From MongoDB 3.0 onwards, you need to use findAndModify and the fields options also you need to set new to true in other to return the new value.

db.collection.findAndModify({   
    query: { 
        "_id": ObjectId("56d6a7292c06e85687f44541"), 
        "rankings._id" : ObjectId("46d6a7292c06e85687f55543") 
    },     
    update: { $inc : { "rankings.$.score" : 1 } },       
    new: true,  
    fields: { 
        "rankings": { 
            "$elemMatch": { "_id" : ObjectId("46d6a7292c06e85687f55543") }
        }  
    }
})

两个查询都产生了:

{
        "_id" : ObjectId("56d6a7292c06e85687f44541"),
        "rankings" : [
                {
                        "_id" : ObjectId("46d6a7292c06e85687f55543"),
                        "name" : "Ranking 2",
                        "score" : 11
                }
        ]
}

这篇关于如何在嵌入式数组中更新后取回新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆