
- 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
Perform conditional upserts or updates in MongoDB
For conditional upserts or updates, you can use $max operator. Let us first create a collection with documents
>db.conditionalUpdatesDemo.insertOne({"_id":100,"StudentFirstScore":89,"StudentSecondScore":78,"BiggestScore":89}); { "acknowledged" : true, "insertedId" : 100 } >db.conditionalUpdatesDemo.insertOne({"_id":101,"StudentFirstScore":305,"StudentSecondScore":560,"BiggestScore":1050}); { "acknowledged" : true, "insertedId" : 101 } >db.conditionalUpdatesDemo.insertOne({"_id":103,"StudentFirstScore":560,"StudentSecondScore":789,"BiggestScore":880}); { "acknowledged" : true, "insertedId" : 103 } Following is the query to display all documents from a collection with the help of find() method: > db.conditionalUpdatesDemo.find().pretty();
This will produce the following output
{ "_id" : 100, "StudentFirstScore" : 89, "StudentSecondScore" : 78, "BiggestScore" : 89 } { "_id" : 101, "StudentFirstScore" : 305, "StudentSecondScore" : 560, "BiggestScore" : 1050 } { "_id" : 103, "StudentFirstScore" : 560, "StudentSecondScore" : 789, "BiggestScore" : 880 }
Following is the query for conditional upserts or updates in MongoDB
> db.conditionalUpdatesDemo.update( { _id: 100 }, { $max: { "BiggestScore": 150 } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the field “BiggestScore” is updated with value 150 or not for _id 100
> db.conditionalUpdatesDemo.find().pretty();
This will produce the following output
{ "_id" : 100, "StudentFirstScore" : 89, "StudentSecondScore" : 78, "BiggestScore" : 150 } { "_id" : 101, "StudentFirstScore" : 305, "StudentSecondScore" : 560, "BiggestScore" : 1050 } { "_id" : 103, "StudentFirstScore" : 560, "StudentSecondScore" : 789, "BiggestScore" : 880 }
- Related Articles
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- Multiple atomic updates using MongoDB?
- Conditional $first in MongoDB aggregation ignoring NULL?
- How to do conditional update in MongoDB?
- How to perform conditional GROUP BY in MySQL to fetch?
- Conditional update depending on field matched in MongoDB
- Perform aggregation sort in MongoDB?
- Perform simple validation in MongoDB?
- Perform Bulk insert in MongoDB?
- Conditional upsert (multiple insert) when updating document in MongoDB?
- Perform MongoDB full text search
- Perform nested document value search in MongoDB?
- How to perform ascending order sort in MongoDB?
- How to perform descending order sort in MongoDB?
- Perform min/max with MongoDB aggregation

Advertisements