You can use $set operator for this. The syntax is as follows −
db.yourCollectionName.update({ }, {'$set': "yourFieldName": "yourValue" }, false, true);
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.unconditionalUpdatesDemo.insertOne({"ClientName":"Larry","ClientAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c8eb7372f684a30fbdfd557") } > db.unconditionalUpdatesDemo.insertOne({"ClientName":"Mike","ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c8eb73f2f684a30fbdfd558") } > db.unconditionalUpdatesDemo.insertOne({"ClientName":"Sam","ClientAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c8eb7462f684a30fbdfd559") } > db.unconditionalUpdatesDemo.insertOne({"ClientName":"Carol","ClientAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c8eb7502f684a30fbdfd55a") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.unconditionalUpdatesDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8eb7372f684a30fbdfd557"), "ClientName" : "Larry", "ClientAge" : 24 } { "_id" : ObjectId("5c8eb73f2f684a30fbdfd558"), "ClientName" : "Mike", "ClientAge" : 26 } { "_id" : ObjectId("5c8eb7462f684a30fbdfd559"), "ClientName" : "Sam", "ClientAge" : 27 } { "_id" : ObjectId("5c8eb7502f684a30fbdfd55a"), "ClientName" : "Carol", "ClientAge" : 29 }
Here is the query for unconditional updates −
> db.unconditionalUpdatesDemo.update({ }, {'$set': {"ClientName": "Robert" }}, false, true); WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
Let us check the documents from a collection with the help find(). The query is as follows −
> db.unconditionalUpdatesDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8eb7372f684a30fbdfd557"), "ClientName" : "Robert", "ClientAge" : 24 } { "_id" : ObjectId("5c8eb73f2f684a30fbdfd558"), "ClientName" : "Robert", "ClientAge" : 26 } { "_id" : ObjectId("5c8eb7462f684a30fbdfd559"), "ClientName" : "Robert", "ClientAge" : 27 } { "_id" : ObjectId("5c8eb7502f684a30fbdfd55a"), "ClientName" : "Robert", "ClientAge" : 29 }