I'm having difficulty with the syntax for adding a new field across all documents in a MongoDB collection. Any ideas where I'm going wrong?
For example, I would like to add an empty Array to all documents in a collection called 'cats':
db.cats.updateMany({}, {$set: {images: []}});
It returns the following in AWS Cloud9 command line and doesn't update in MongoDB Atlas documents:
MongoDB Enterprise **db**-shard-0:PRIMARY> db.cats.updateMany({}, {$set: {images: []}});
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }
I'm using MongoDB shell version v3.6.3 (however my Server version is 4.2.8)
You have to use empty selector, and set a multi flag to true (last param) to update all the documents
db.getCollection('cats').update(
{},
{ $set: {"images": []} },
false,
true
)