- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 to push an array in MongoDB?
To push an array, use $push in MongoDB. Let us first create a collection with documents −
> db.demo399.insertOne({Name:"Chris",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5e610339fac4d418a017856d") } > db.demo399.insertOne({Name:"David",Age:22}); { "acknowledged" : true, "insertedId" : ObjectId("5e610341fac4d418a017856e") } > db.demo399.insertOne({Name:"Chris",Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5e610355fac4d418a017856f") } > db.demo399.insertOne({Name:"Bob",Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5e61035efac4d418a0178570") } > db.demo399.insertOne({Name:"David",Age:22}); { "acknowledged" : true, "insertedId" : ObjectId("5e610364fac4d418a0178571") }
Display all documents from a collection with the help of find() method −
> db.demo399.find();
This will produce the following output −
{ "_id" : ObjectId("5e610339fac4d418a017856d"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e610341fac4d418a017856e"), "Name" : "David", "Age" : 22 } { "_id" : ObjectId("5e610355fac4d418a017856f"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e61035efac4d418a0178570"), "Name" : "Bob", "Age" : 23 } { "_id" : ObjectId("5e610364fac4d418a0178571"), "Name" : "David", "Age" : 22 }
Following is the query to push an array −
> db.demo399.aggregate( ... [ ... { ... $group: ... { ... _id: null, ... array: { $push: { Name: "$Name", Age: "$Age" } } ... } ... } ... ] ... )
This will produce the following output −
{ "_id" : null, "array" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 22 }, { "Name" : "Chris", "Age" : 21 }, { "Name" : "Bob", "Age" : 23 }, { "Name" : "David", "Age" : 22 } ] }
- Related Articles
- How to push an element into array in MongoDB?
- Updating an array with $push in MongoDB
- How do I push elements to an existing array in MongoDB?
- MongoDB query to push document into an array
- How to push new items to an array inside of an object in MongoDB?
- Cannot push into an array from MongoDB?
- MongoDB $push in nested array?
- Update an array element matching a condition using $push in MongoDB
- Query an array of embedded documents in MongoDB and push another?
- Implement MongoDB $push in embedded document array?
- MongoDB query to update an array element matching a condition using $push?
- How to push an element to the last of an array in TypeScript?
- How to push an element at the beginning of an array in TypeScript?
- Golang Program To Push An Array Into Another Array
- C++ Program to push an array into another array

Advertisements