Answers for "add a new field to all documents mongodb"

4

how to add a new propety into all documents in mongodb

db.users.update({}, { "$set" : { "age": 30 }}, false,true)
// users: collection name, age: new property
//false it's upsert argument, it tells mongo to not insert a new document when no match is found
// true it's multi argument, it tells mongo to update multiple documents that meet the query criteria
Posted by: Guest on September-17-2020
0

mongodb add new field

// If you want to add a new_field to all your collection, you have to use empty
// selector, and set multi flag to true (last param) to update all the documents

db.your_collection.update(
  {},
  { $set: {"new_field": 1} },
  false,
  true
)
// In the above example last 2 fields false, true specifies the upsert and multi flags.
// Upsert: If set to true, creates a new document when no document matches the query criteria.
Posted by: Guest on August-03-2021

Code answers related to "add a new field to all documents mongodb"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language