鑑於以下MongoDB的集合:如何在嵌入式數組更新後獲取新值?
{
"_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
},
]
}
這是我如何增加給定名次的得分:
db.collection.update(
{ "_id": ObjectId("56d6a7292c06e85687f44541"), "rankings._id" : ObjectId("46d6a7292c06e85687f55543") },
{ $inc : { "rankings.$.score" : 1 } }
);
我如何獲得新的得分值?在之前的查詢中,我將第二個排名從10增加到了11 ...如何在更新後獲取此新值?
你可以使用['findAndModify'(https://docs.mongodb.org/manual/reference/command/findAndModify/) – BatScream