
- 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
MongoDB $push in nested array?
Here, $push can be used to add new documents in nested array. To understand the above $push concept, let us create a collection with nested array document. The query to create a collection with document is as follows:
>db.nestedArrayDemo.insertOne({"EmployeeName":"Larry","EmployeeSalary":9000,"EmployeeDetails": [{"EmployeeDOB":new Date('1990-01-21'),"EmployeeDepartment":"ComputerScience","EmployeeProject": [{"Technology":"C","Duration":6},{"Technology":"Java","Duration":7}]}]});
The following is the output:
{ "acknowledged" : true, "insertedId" : ObjectId("5c6d73090c3d5054b766a76e") }
Now you can display documents from a collection with the help of find() method. The query is as follows:
> db.nestedArrayDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6d73090c3d5054b766a76e"), "EmployeeName" : "Larry", "EmployeeSalary" : 9000, "EmployeeDetails" : [ { "EmployeeDOB" : ISODate("1990-01-21T00:00:00Z"), "EmployeeDepartment" : "ComputerScience", "EmployeeProject" : [ { "Technology" : "C", "Duration" : 6 }, { "Technology" : "Java", "Duration" : 7 } ] } ] }
Here is the demo of $push in nested array to add new documents. The query is as follows:
>db.nestedArrayDemo.update({"_id":ObjectId("5c6d73090c3d5054b766a76e"), "EmployeeDetails.EmployeeDepartment":"ComputerScience"}, {"$push": {"EmployeeDetails.$.EmployeeProject": {"Technology":"Python", "Duration":4 }}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
In the above query, I have added a document {"Technology":"Python", "Duration":4 } in nested array. Now display the documents from the collection once again. The query is as follows:
> db.nestedArrayDemo.find().pretty();
The following is the output:
{ "_id" : ObjectId("5c6d73090c3d5054b766a76e"), "EmployeeName" : "Larry", "EmployeeSalary" : 9000, "EmployeeDetails" : [ { "EmployeeDOB" : ISODate("1990-01-21T00:00:00Z"), "EmployeeDepartment" : "ComputerScience", "EmployeeProject" : [ { "Technology" : "C", "Duration" : 6 }, { "Technology" : "Java", "Duration" : 7 }, { "Technology" : "Python", "Duration" : 4 } ] } ] }
- Related Articles
- Implement MongoDB $push in embedded document array?
- How to push an array in MongoDB?
- Updating an array with $push in MongoDB
- Cannot push into an array from MongoDB?
- Clearing items in a nested MongoDB array?
- How to push an element into array in MongoDB?
- MongoDB Increment value inside nested array?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- MongoDB query to push document into an array
- How to create double nested array in MongoDB?
- Retrieve values from nested JSON array in MongoDB?
- Set filtering conditions for nested array in MongoDB
- Increment MongoDB value inside a nested array
- Query array of nested string with MongoDB?

Advertisements