
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Resolve ‘multi update only works with $ operators’ in MongoDb?
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 }
- Related Articles
- Update values in multiple documents with multi parameter in MongoDB?
- Update only specific fields in MongoDB?
- MongoDB query to update a MongoDB row with only the objectid
- Update only a single document in MongoDB
- How to update only one property in MongoDB?
- MongoDB query to update only certain fields?
- Update only a specific value in a MongoDB document
- Does aggregation query with $match works in MongoDB?
- Only insert if a value is unique in MongoDB else update
- Update only a single MongoDB document without deleting any date
- MongoDB concurrent update with sub collection?
- Update two separate arrays in a document with one update call in MongoDB?
- Update MongoDB variable value with variable itself?
- How to update array with multiple conditions in MongoDB
- Update the last row with search criteria in MongoDB?

Advertisements