Hello,
I am trying to add a field to all the documents in my collection which has >100,000 documents. There is no filter for this operation. Is there any better API that I can use other than collection.updateMany() to ensure that the operation is faster?
Thanks,
Parikshit
steevej
(Steeve Juneau)
May 11, 2021, 12:18pm
2
Not that I know off. For example, you can add/set/update the field *verified to the current date and add/set/update the field user with:
// collection before
mongo shell> db.Parikshit.find()
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : 3 }
// what to update all documents
mongo shell> query = {}
// the operations we want to perform
mongo shell> operations = {
"$currentDate" : {
"verified" : {
"$type" : "date"
}
},
"$set" : {
"user" : "steevej"
}
}
mongo shell> db.Parikshit.updateMany( query , operations )
// the result
mongo shell> db.Parikshit.find()
{ "_id" : 1, "user" : "steevej", "verified" : ISODate("2021-05-11T11:50:56.505Z") }
{ "_id" : 2, "user" : "steevej", "verified" : ISODate("2021-05-11T11:50:56.505Z") }
{ "_id" : 3, "user" : "steevej", "verified" : ISODate("2021-05-11T11:50:56.505Z") }