I have a large DB with various inconsistencies. One of the items I would like to clear up is changing the country status based on the population.

A Sample of the data is:

{ "_id" : "D", "name" : "Deutschland", "pop" : 70000000, "country" : "Large Western" }
{ "_id" : "E", "name" : "Eire", "pop" : 4500000, "country" : "Small Western" }
{ "_id" : "G", "name" : "Greenland", "pop" : 30000, "country" : "Dependency" }
{ "_id" : "M", "name" : "Mauritius", "pop" : 1200000, "country" : "Small island"}
{ "_id" : "L", "name" : "Luxembourg", "pop" : 500000, "country" : "Small Principality" }

Obviously I would like to change the country field go something more uniform, based on population size.

I've tried this approach, but obviously missing some way of tying into an update of the country field.

db.country.updateMany( { case : { $lt : ["$pop" : 20000000] }, then : "Small country" }, { case : { $gte : ["$pop" : 20000000] }, then : "Large country" }

Edit: Posted before I was finished writing.

I was thinking to use $cond functionality, to basically return if true, do X, if false, do y, while using the updateMany.

Is this possible, or is there a workaround?

1 Answers

3
Neil Lunn On Best Solutions

You really want want bulkWrite() using two "updateMany" statements within it instead. Aggregation expressions cannot be used to do "alternate selection" in any form of update statement.

db.country.bulkWrite([
  { "updateMany": {
    "filter": { "pop": { "$lt": 20000000 } },
    "update": { "$set": { "country": "Small Country" } }
  }},
  { "updateMany": {
    "filter": { "pop": { "$gt": 20000000 } },
    "update": { "$set": { "country": "Large Country" } } 
  }}
])

There is still an outstanding "feature request" on SERVER-6566 for "conditional syntax", but this is not yet resolved. The "bulk" API was actually introduced after this request was raised, and really can be adapted as shown to do more or less the same thing.

Also using $out in an aggregation statement as was otherwise suggested is not an option to "update" and can only write to a "new collection" at present. The slated change from MongoDB 4.2 onwards would allow $out to actually "update" an existing collection, however this would only be where the collection to be updated is different from any other collection used within the gathering of data from the aggregation pipeline. So it is not possible to use an aggregation pipeline to update the same collection as what you are reading from.

In short, use bulkWrite().