- 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
Updating an array with $push in MongoDB
To update an array with $push, use updateOne() in MongoDB. Let us create a collection with documents −
> db.demo526.insertOne( ... { ... ... "CountryName": "US", ... "TeacherName": "Bob", ... "StudentInformation": [ ... { ... "Name": "Chris", ... "Subject": "MySQL", ... "ListOfMailId":[] ... }, ... { ... "Name": "David", ... "Subject": "MongoDB", ... "ListOfMailId":[] ... ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8af031437efc8605595b6b") }
Display all documents from a collection with the help of find() method −
> db.demo526.find();
This will produce the following output −
{ "_id" : ObjectId("5e8af031437efc8605595b6b"), "CountryName" : "US", "TeacherName" : "Bob", "StudentInformation" : [ { "Name" : "Chris", "Subject" : "MySQL", "ListOfMailId" : [ ] }, { "Name" : "David", "Subject" : "MongoDB", "ListOfMailId" : [ ] } ] }
Following is the query to update an array with $push −
> db.demo526.updateOne( ... { ... _id:ObjectId("5e8af031437efc8605595b6b"), ... "StudentInformation": { "$elemMatch": { "Name": "David", "Subject": "MongoDB" }} ... }, ... { ... "$push": { "StudentInformation.$.ListOfMailId": { "MailId": "David@gmail.com" }} ... ... } ... ) { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo526.find();
This will produce the following output −
{ "_id" : ObjectId("5e8af031437efc8605595b6b"), "CountryName" : "US", "TeacherName" : "Bob", "StudentInformation" : [ { "Name" : "Chris", "Subject" : "MySQL", "ListOfMailId" : [ ] }, { "Name" : "David", "Subject" : "MongoDB", "ListOfMailId" : [ { "MailId" : "David@gmail.com" } ] } ] }
- Related Articles
- How to push an array in MongoDB?
- Cannot push into an array from MongoDB?
- MongoDB $push in nested array?
- How to push an element into array in MongoDB?
- MongoDB query to push document into an array
- Work with $push in MongoDB
- MongoDB syntax for updating an object inside an array within a document?
- Update an array element matching a condition using $push in MongoDB
- How do I push elements to an existing array in MongoDB?
- Query an array of embedded documents in MongoDB and push another?
- Creating an associative array in JavaScript with push()?
- Implement MongoDB $push in embedded document array?
- How to push new items to an array inside of an object in MongoDB?
- Update objects in a MongoDB documents array (nested updating)?
- MongoDB query to update an array element matching a condition using $push?

Advertisements