

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Combine update and query parts to form the upserted document in MongoDB?
<p>You need to use $set operator along with upsert:true. Let us first create a collection with documents −</p><pre class="prettyprint notranslate">> db.updateWithUpsertDemo.insertOne({"StudentFirstName":"John","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a61c345990cee87fd890") } > db.updateWithUpsertDemo.insertOne({"StudentFirstName":"Larry","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a624345990cee87fd891") } > db.updateWithUpsertDemo.insertOne({"StudentFirstName":"David","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2a62c345990cee87fd892") }</pre><p>Following is the query to display all documents from a collection with the help of find() method −</p><pre class="prettyprint notranslate">> db.updateWithUpsertDemo.find().pretty();</pre><p>This will produce the following output −</p><pre class="result notranslate">{ "_id" : ObjectId("5cd2a61c345990cee87fd890"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd2a624345990cee87fd891"), "StudentFirstName" : "Larry", "StudentAge" : 23 } { "_id" : ObjectId("5cd2a62c345990cee87fd892"), "StudentFirstName" : "David", "StudentAge" : 24 }</pre><p>Following is the query to combine update and query parts to form the upserted document −</p><pre class="prettyprint notranslate">> db.updateWithUpsertDemo.update({_id: ObjectId("5cd2a624345990cee87fd891")},{"$set": {"StudentFirstName": "Chris"}}, {upsert:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })</pre><p>Let us check whether the field “StudentFirstName” has been changed or not −</p><pre class="prettyprint notranslate">> db.updateWithUpsertDemo.find().pretty();</pre><p>This will produce the following output −</p><pre class="result notranslate">{ "_id" : ObjectId("5cd2a61c345990cee87fd890"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd2a624345990cee87fd891"), "StudentFirstName" : "Chris", "StudentAge" : 23 } { "_id" : ObjectId("5cd2a62c345990cee87fd892"), "StudentFirstName" : "David", "StudentAge" : 24 }</pre>
- Related Questions & Answers
- MongoDB query to update the nested document?
- MongoDB query to update nested document
- MongoDB Query to combine AND & OR?
- MongoDB query to update a specific document from a collection
- Display MongoDB with document and subdocument example and update
- MongoDB query to update tag
- MongoDB findOneAndUpdate() to update a single document
- How to update the _id of a MongoDB Document?
- Update only a single document in MongoDB
- Update a MongoDB document with Student Id and Name
- MongoDB query to update selected fields
- MongoDB query with fields in the same document?
- How do you update a MongoDB document while replacing the entire document?
- MongoDB query to return only embedded document?
- MongoDB query to get last inserted document?
Advertisements