1

I'm trying to preserve the collections that do have the property set, but I need to define it for those that don't have it.

db.getCollection('accounts').update({
 deposit: { "$exists": false } },
 { $set: {
     deposit: { currency: '', address: '' }
 }
})
chovy
  • 72,281
  • 52
  • 227
  • 295

1 Answers1

0

To update all documents in the collection add the {multi: true} as 3rd parameter of the update method:

db.getCollection('accounts').update(
  {
    deposit: { "$exists": false } 
  },
  { 
    $set: {
      deposit: { currency: '', address: '' }
    }
  },
  { 
    multi: true 
  }
);

See https://docs.mongodb.com/manual/reference/method/db.collection.update/ for more details.

JME
  • 483
  • 2
  • 5