
- 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
How to add new item in nested array with MongoDB?
For this, use find() along with update(). Let us create a collection with documents −
> db.demo124.insertOne( ... { ... "Name" : "John", ... "Id" : 101, ... "ProjectDetails" : [{ ... "ProjectName1" : "Online Book", ... "ProjectName2" : "Online Banking" ... }, { ... "ProjectName1" : "Online Library Management System", ... "ProjectName2" : "School Management System" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2f2c8b140daf4c2a3544bb") }
Display all documents from a collection with the help of find() method −
> db.demo124.find();
This will produce the following output −
{ "_id" : ObjectId("5e2f2c8b140daf4c2a3544bb"), "Name" : "John", "Id" : 101, "ProjectDetails" : [ { "ProjectName1" : "Online Book", "ProjectName2" : "Online Banking" }, { "ProjectName1" : "Online Library Management System", "ProjectName2" : "School Management System" } ] }
Following is the query to add new item in nested array −
> db.demo124.find().toArray().forEach( ... function(d){ ... for(var i = 0; i< d.ProjectDetails.length; ++i) { ... d.ProjectDetails[i]['ProjectName3'] = 'Online Snake Game'; ... } ... db.demo124.update({_id: d._id}, d); ... } ... );
Display all documents from a collection with the help of find() method −
> db.demo124.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e2f2c8b140daf4c2a3544bb"), "Name" : "John", "Id" : 101, "ProjectDetails" : [ { "ProjectName1" : "Online Book", "ProjectName2" : "Online Banking", "ProjectName3" : "Online Snake Game" }, { "ProjectName1" : "Online Library Management System", "ProjectName2" : "School Management System", "ProjectName3" : "Online Snake Game" } ] }
- Related Articles
- MongoDB query to add new array element in document
- How to update a MongoDB document for adding a new item to an array?
- MongoDB $addToSet to add a deep nested array of object?
- How to create double nested array in MongoDB?
- Query array of nested string with MongoDB?
- How to group nested fields in MongoDB aggregation with count value in array?
- MongoDB $push in nested array?
- MongoDB query to aggregate nested array
- MongoDB query to sort nested array?
- Removing item from array in MongoDB?
- MongoDB query to remove item from array?
- MongoDB query to gather unique array item?
- Query a nested field within an array with MongoDB
- Search for documents matching first item in an array with MongoDB?
- Remove item from a nested array by indices in JavaScript

Advertisements