

- 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
How can I update child objects in MongoDB?
To update child objects, use $set operator. Let us first create a collection with document −
>db.updateChildObjectsDemo.insertOne({"StudentName":"Chris","StudentOtherDetails":{"StudentSubject":"MongoDB","StudentCountryName":"AUS"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ce964e078f00858fb12e91f") }
Following is the query to display all documents from a collection with the help of find() method −
> db.updateChildObjectsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce964e078f00858fb12e91f"), "StudentName" : "Chris", "StudentOtherDetails" : { "StudentSubject" : "MongoDB", "StudentCountryName" : "AUS" } }
Following is the query to update child objects in MongoDB −
> db.updateChildObjectsDemo.update({"StudentName" : "Chris"},{$set:{"StudentOtherDetails.StudentCountryName":"UK"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document once again −
> db.updateChildObjectsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce964e078f00858fb12e91f"), "StudentName" : "Chris", "StudentOtherDetails" : { "StudentSubject" : "MongoDB", "StudentCountryName" : "UK" } }
- Related Questions & Answers
- How can I update child objects in MongoDB database?
- How can I update and increment two fields in one command in MongoDB?
- How can we update a record in MongoDB?
- Update objects in a MongoDB documents array (nested updating)?
- How can I store JavaScript objects in cookies?
- How can I remove a child node in HTML using JavaScript?
- How can I update the boolean values in MySQL?
- How can I serialize python objects to XML?
- How can I make my custom objects Parcelable?
- How can I make my custom objects Serializable?
- How do I $set and $push in single update with MongoDB?
- How can I update a table using prepare statements?
- How can I filter JSON data with multiple objects?
- How can I rename a collection in MongoDB?
- How can I save new Date() in MongoDB?
Advertisements