
- 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
Update an array element matching a condition using $push in MongoDB
For this, use update command and $push. Let us first create a collection with documents −
>db.demo9.insertOne({"StudentDetails":[{"StudentName":"Chris","ListOfSubject":["MySQL","Java"]}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f6438d7df943a7cec4f94") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo9.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e0f6438d7df943a7cec4f94"), "StudentDetails" : [ { "StudentName" : "Chris", "ListOfSubject" : [ "MySQL", "Java" ] } ] }
Following is the query to update an array element matching a condition using $push −
> db.demo9.update( { "StudentDetails.StudentName":"Chris"}, {$push:{"StudentDetails.$.ListOfSubject":"MongoDB"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Following is the query to display all documents from a collection with the help of find() method −
> db.demo9.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e0f6438d7df943a7cec4f94"), "StudentDetails" : [ { "StudentName" : "Chris", "ListOfSubject" : [ "MySQL", "Java", "MongoDB" ] } ] }
- Related Articles
- MongoDB query to update an array element matching a condition using $push?
- How to push an element into array in MongoDB?
- Update Array element in MongoDB?
- Removing an array element from MongoDB collection using update() and $pull
- MongoDB query to insert an array element with a condition?
- How to push an array in MongoDB?
- Updating an array with $push in MongoDB
- MongoDB query to update an array using FindAndUpdate()?
- Update field in exact element array in MongoDB?
- Convert a field to an array using update operation in MongoDB
- Convert a field to an array using MongoDB update operation?
- Cannot push into an array from MongoDB?
- MongoDB $push in nested array?
- Update elements inside an array in MongoDB?
- MongoDB query to push document into an array

Advertisements