
- 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
Conditional upsert (multiple insert) when updating document in MongoDB?
For multiple write operations, use bulkWrite() in MongoDB. Let us create a collection with documents −
> db.demo428.insertOne({ "Name" : "Chris", "Age" : 21 }); { "acknowledged" : true, "insertedId" : ObjectId("5e75f428bbc41e36cc3cae83") } > db.demo428.insertOne({ "Name" : "Chris", "Age" : 23 }); { "acknowledged" : true, "insertedId" : ObjectId("5e75f429bbc41e36cc3cae84") } > db.demo428.insertOne({ "Name" : "David", "Age" : 22 }); { "acknowledged" : true, "insertedId" : ObjectId("5e75f42abbc41e36cc3cae85") } > db.demo428.insertOne({ "Name" : "David", "Age" : 21 }); { "acknowledged" : true, "insertedId" : ObjectId("5e75f42abbc41e36cc3cae86") }
Display all documents from a collection with the help of find() method
> db.demo428.find();
This will produce the following output −
{ "_id" : ObjectId("5e75f428bbc41e36cc3cae83"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e75f429bbc41e36cc3cae84"), "Name" : "Chris", "Age" : 23 } { "_id" : ObjectId("5e75f42abbc41e36cc3cae85"), "Name" : "David", "Age" : 22 } { "_id" : ObjectId("5e75f42abbc41e36cc3cae86"), "Name" : "David", "Age" : 21 }
Following is the query for conditional upsert (insert) when updating document in MongoDB −
> db.demo428.bulkWrite( ... [ ... { "updateOne": { ... "filter": { "Name": "David", "Age": 22 }, ... "update": { "$set": { "Info": {Name:"John"} } } ... }}, ... { "insertOne": { ... "document": { "Name": "Carol", "Age": 22, "Info": {Name:"John"}} ... }} ... ], ... { "ordered": false } ... ) { "acknowledged" : true, "deletedCount" : 0, "insertedCount" : 1, "matchedCount" : 1, "upsertedCount" : 0, "insertedIds" : { "1" : ObjectId("5e75f448bbc41e36cc3cae87") }, "upsertedIds" : { } }
Display all documents from a collection with the help of find() method −
> db.demo428.find();
This will produce the following output −
{ "_id" : ObjectId("5e75f428bbc41e36cc3cae83"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e75f429bbc41e36cc3cae84"), "Name" : "Chris", "Age" : 23 } { "_id" : ObjectId("5e75f42abbc41e36cc3cae85"), "Name" : "David", "Age" : 22, "Info" : { "Name" : "John" } } { "_id" : ObjectId("5e75f42abbc41e36cc3cae86"), "Name" : "David", "Age" : 21 } { "_id" : ObjectId("5e75f448bbc41e36cc3cae87"), "Name" : "Carol", "Age" : 22, "Info" : { "Name" : "John" } }
- Related Articles
- Updating nested document in MongoDB
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- How to insert multiple document into a MongoDB collection using Java?
- Insert MongoDB document field only when it's missing?
- Updating a MongoDB document and adding new keys only in the first document?
- Upsert many documents in MongoDB
- How to use custom variable while updating a MongoDB document?
- How to insert a document with date in MongoDB?
- MongoDB syntax for updating an object inside an array within a document?
- Updating data in MongoDB
- Writing a MongoDB insert statement for multiple insert at a time
- Updating sub-object in MongoDB?
- MongoDB query for exact match on multiple document fields
- How to insert a document into a MongoDB collection using Java?
- Updating Nested Embedded Documents in MongoDB?

Advertisements