2013-05-27 48 views
0

我有一个'位置'集合,我想用'united + states'替换'usa'。数据库的当前状态为:MongoDB通过值匹配更新数组元素

> 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) 

不过,这并没有达到预期的影响和集合文件仍然有“美国”而不是“统一+国家”。

+0

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

回答

1

它看起来像你有你的更新调用一个错字,而不是尝试以下:

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

注:选择现在{multi:true}而非multi=true(或只是true

+0

+1工作正常 – arthankamal

+0

是的,我在那里犯了一个错误。感谢您指出了这一点。 – VaidAbhishek

0

更新多个数组元素是不可能的,请在此回答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); 
});