
- 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 query to push document into an array
To push document into an array, use $push along with update(). Let us create a collection with documents −
>db.demo310.insertOne({"Name":"Chris","details":[{"Id":101,"Subject":"MySQL"},{"Id":102,"Subject":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cabdf8647eb59e562043") }
Display all documents from a collection with the help of find() method −
> db.demo310.find();
This will produce the following output −
{ "_id" : ObjectId("5e50cabdf8647eb59e562043"), "Name" : "Chris", "details" : [ { "Id" : 101, "Subject" : "MySQL" }, { "Id" : 102, "Subject" : "MongoDB" } ] }
Following is the query to push document −
> db.demo310.update({ _id:ObjectId("5e50cabdf8647eb59e562043")}, ...{ $push: {"details": { ... "Id" : 103, ... "Subject": "Java" ... }} ...} ...) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo310.find();
This will produce the following output −
{ "_id" : ObjectId("5e50cabdf8647eb59e562043"), "Name" : "Chris", "details" : [ { "Id" : 101, "Subject" : "MySQL" }, { "Id" : 102, "Subject" : "MongoDB" }, { "Id" : 103, "Subject" : "Java" } ] }
- Related Articles
- Cannot push into an array from MongoDB?
- How to push an element into array in MongoDB?
- Implement MongoDB $push in embedded document array?
- Push query results into variable with MongoDB?
- MongoDB query to update an array element matching a condition using $push?
- Query an array of embedded documents in MongoDB and push another?
- How to query a document in MongoDB comparing fields from an array?
- How to push an array in MongoDB?
- MongoDB query to remove array elements from a document?
- MongoDB query to add new array element in document
- MongoDB aggregate to convert multiple documents into single document with an array?
- Golang Program To Push An Array Into Another Array
- C++ Program to push an array into another array
- Python Program to push an array into another array
- Updating an array with $push in MongoDB

Advertisements