【问题标题】:MongoDB updating an Array Element by value matchingMongoDB 通过值匹配更新数组元素
【发布时间】:2013-05-27 19:17:12
【问题描述】:

我有一个集合'locations',我想用'united+states'替换'usa'。 DB目前的状态如下:

> db.locations.find({'country':'usa'})
{ "_id" : "boston", "country" : [ "usa" ], "server" : "", "ts" : 1369642586.077319, "type" : [ "city" ] }
{ "_id" : "chicago", "country" : [ "usa" ], "server" : "", "ts" : 1369642589.226037, "type" : [ "city" ] }
{ "_id" : "las+vegas", "country" : [ "usa" ], "server" : "", "ts" : 1369642591.688284, "type" : [ "city" ] }
{ "_id" : "los+angeles", "country" : [ "usa" ], "server" : "", "ts" : 1369642601.672113, "type" : [ "city" ] }
{ "_id" : "miami", "country" : [ "usa" ], "server" : "", "ts" : 1369642606.281971, "type" : [ "city" ] }
{ "_id" : "new+york", "country" : [ "usa" ], "server" : "", "ts" : 1369642611.884712, "type" : [ "city" ] }
{ "_id" : "portland", "country" : [ "usa" ], "server" : "", "ts" : 1369642615.59296, "type" : [ "city" ] }
{ "_id" : "san+francisco", "country" : [ "usa" ], "server" : "2", "ts" : 1369642619.255885, "type" : [ "city" ] }
{ "_id" : "san+jose", "country" : [ "usa" ], "server" : "", "ts" : 1369642622.74329, "type" : [ "city" ] }
{ "_id" : "seattle", "country" : [ "usa" ], "server" : "", "ts" : 1369642625.195549, "type" : [ "city" ] }
{ "_id" : "washington+dc", "country" : [ "usa" ], "server" : "", "ts" : 1369642628.37334, "type" : [ "city" ] }
{ "_id" : "cleveland", "country" : [ "usa" ], "server" : "2", "ts" : 1369642634.523065, "type" : [ "city" ] }
{ "_id" : "columbus", "country" : [ "usa" ], "server" : "2", "ts" : 1369642636.874618, "type" : [ "city" ] }
{ "_id" : "dallas", "country" : [ "usa" ], "server" : "2", "ts" : 1369642639.080141, "type" : [ "city" ] }
{ "_id" : "denver", "country" : [ "usa" ], "server" : "2", "ts" : 1369642642.236581, "type" : [ "city" ] }
{ "_id" : "houston", "country" : [ "usa" ], "server" : "2", "ts" : 1369642644.783804, "type" : [ "city" ] }
{ "_id" : "minneapolis", "country" : [ "usa" ], "server" : "2", "ts" : 1369642647.153817, "type" : [ "city" ] }
{ "_id" : "nashville", "country" : [ "usa" ], "server" : "2", "ts" : 1369642650.497276, "type" : [ "city" ] }
{ "_id" : "new+orleans", "country" : [ "usa" ], "server" : "2", "ts" : 1369642653.584663, "type" : [ "city" ] }
{ "_id" : "philadelphia", "country" : [ "usa" ], "server" : "2", "ts" : 1369642656.524054, "type" : [ "city" ] }

我运行了以下 MongoDB 查询,应该可以解决问题:

db.locations.update({'country':'usa'}, {'$set': {'country.$': 'united+states'}}, multi=true)

但是,这并没有达到预期的效果,并且 Collection 文档仍然有 'usa' 而不是 'united+states'。

【问题讨论】:

  • 你有没有交叉检查。默认情况下,更新只更新一个文档,而不是所有匹配的文档。您的更新可能有效 - 只需检查 db.locations.count({'country':'usa'}) 在更新前后是否返回相同的值。

标签: mongodb mongodb-update


【解决方案1】:

您的更新调用中似乎有错字,请尝试以下操作:

db.locations.update({'country':'usa'}, {'$set': {'country.$': 'united+states'}}, {multi:true})

注意:options 现在是 {multi:true} 而不是 multi=true(或只是 true

【讨论】:

    【解决方案2】:

    更新多个数组元素是不可能的,检查这个答案MongoDB update multiple records of array,如果他们是正确的,这可能会起作用

    db.locations.find().forEach(function(doc) { 
        do { 
           db.locations.update({'country' : 'usa'}, 
           { $set : {'country.$' : 'united states'}}, true); 
        } while(db.getPrevError().n != 0); 
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2013-09-07
      • 2011-12-19
      • 2021-04-16
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多