
- 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 insert an item to an array that is inside an object in MongoDB?
To insert an item to an already created array inside an object, use MongoDB $push. Let us create a collection with documents −
> db.demo449.insertOne( ... { ... details1: { ... details2: [{ ... _id:new ObjectId(), ... Name:"Chris" ... }], ... details3: [{ ... _id:new ObjectId(), ... Name:"David" ... }] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e7a40e971f552a0ebb0a6e3") }
Display all documents from a collection with the help of find() method −
> db.demo449.find();
This will produce the following output −
{ "_id" : ObjectId("5e7a40e971f552a0ebb0a6e3"), "details1" : { "details2" : [ { "_id" : ObjectId("5e7a40e971f552a0ebb0a6e1"), "Name" : "Chris" } ], "details3" : [ { "_id" : ObjectId("5e7a40e971f552a0ebb0a6e2"), "Name" : "David" } ] } }
Following is the query to insert an item to an array that is inside an object −
> db.demo449.update({_id:ObjectId("5e7a40e971f552a0ebb0a6e3")}, {$push: { 'details1.details2':{_id:ObjectId(),"Name":"Carol"}}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo449.find();
This will produce the following output −
{ "_id" : ObjectId("5e7a40e971f552a0ebb0a6e3"), "details1" : { "details2" : [ { "_id" : ObjectId("5e7a40e971f552a0ebb0a6e1"), "Name" : "Chris" }, { "_id" : ObjectId("5e7a41a671f552a0ebb0a6e5"), "Name" : "Carol" } ], "details3" : [ { "_id" : ObjectId("5e7a40e971f552a0ebb0a6e2"), "Name" : "David" } ] } }
- Related Articles
- How to push new items to an array inside of an object in MongoDB?
- MongoDB query to find data from an array inside an object?
- How can I delete an item from an Object in MongoDB?
- MongoDB syntax for updating an object inside an array within a document?
- MongoDB query to access an object in an array
- How to insert an item in ArrayList in C#?
- How to match multiple criteria inside an array with MongoDB?
- How to get the matching document inside an array in MongoDB?
- MongoDB query to set a sub item in an array?
- How to edit values of an object inside an array in a class - JavaScript?
- Update elements inside an array in MongoDB?
- How to insert an image in a Tkinter canvas item?
- How to get items from an object array in MongoDB?
- How to insert an item into a C# list by using an index?
- MongoDB query to insert an array element with a condition?

Advertisements