2013-05-27 47 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); 
});