Set field of all documents with given value only if not existing in Mongo
Use update
method with the multi
flag set to true
db.users.update(
{ "welcomeAck": { "$exists": false } },
{ "$set": { "welcomeAck": true } },
{ "multi": true }
);
Or the equivalent shortcut with updateMany
:
db.users.updateMany(
{ "welcomeAck": { "$exists": false } },
{ "$set": { "welcomeAck": true } }
);
Reference - https://docs.mongodb.com/manual/tutorial/update-documents/
Shared with from Codever.land. 👉 Use the Copy to mine functionality to copy this snippet to your own personal collection and easy manage your code snippets.