使用where条件更新MongoDB中的查询

使用where条件更新MongoDB中的查询,mongodb,Mongodb,上面是我的收藏对象。。我想将which.name更新为“Indian”,其中which.status=1。。请帮助我使用mongoDB查询来执行此操作。您可以尝试以下操作: { "_id" : ObjectId("510902fb7995fe3504000002"), "name" : "Gym", "status" : "1", "whichs" : [ { "name" : "American", "status" : "1" } ] } 这

上面是我的收藏对象。。我想将which.name更新为“Indian”,其中which.status=1。。请帮助我使用mongoDB查询来执行此操作。

您可以尝试以下操作:

{
"_id" : ObjectId("510902fb7995fe3504000002"),
"name" : "Gym",
"status" : "1",
"whichs" : [
    {
        "name" : "American",
        "status" : "1"
    }
]
}
这将查找
“whichs.status”:“1”
,然后为所有文档设置
“whichs.$.name”:“Indian”
。有关update()方法的更多信息,请阅读

db.collection.update({"whichs.status" : "1" }, 
                     {$set : { "whichs.$.name" : "Indian"},
                     false,
                     true);