0

I have a collection 'locations', where I want to replace 'usa' with 'united+states'. The present state of the DB is as following:

> 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" ] }

I ran the following MongoDB query, which should have done the trick:

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

However, this doesn't achieve the desired affect and Collection documents still have 'usa' instead of 'united+states'.

VaidAbhishek
  • 5,895
  • 7
  • 43
  • 59
  • Did you cross check. By default update only updates one document, not all the matching documenting. Your update might have worked - just check if the db.locations.count({'country':'usa'}) returns same value before and after update. – gkamal May 27 '13 at 08:31

2 Answers2

1

It looks like you have a typo in your update call, try the following instead:

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

Note : options is now {multi:true} rather than multi=true (or just true)

jimoleary
  • 1,645
  • 13
  • 11
0

Updating multiple array elements is not possible, check this answers MongoDB update multiple records of array and if they right, may be this'll work

db.locations.find().forEach(function(doc) { 
    do { 
       db.locations.update({'country' : 'usa'}, 
       { $set : {'country.$' : 'united states'}}, true); 
    } while(db.getPrevError().n != 0); 
});
Community
  • 1
  • 1
arthankamal
  • 6,341
  • 4
  • 36
  • 51