
- 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 update an array element matching a condition using $push?
Let us first create a collection with documents −
> db.updateArrayElementDemo.insertOne( { "UserDetails": [ { "UserName":"Chris", "UserAge":23 } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce9029378f00858fb12e90d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.updateArrayElementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce9029378f00858fb12e90d"), "UserDetails" : [ { "UserName" : "Chris", "UserAge" : 23 } ] }
Following is the query to update an array element matching a condition using $push −
db.updateArrayElementDemo.update( {"UserDetails.UserAge": 23}, {"$push": {"UserDetails.$.UserCountryName": "US"}} ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document once again −
> db.updateArrayElementDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ce9029378f00858fb12e90d"), "UserDetails" : [ { "UserName" : "Chris", "UserAge" : 23, "UserCountryName" : [ "US" ] } ] }
- Related Articles
- Update an array element matching a condition using $push in MongoDB
- MongoDB query to insert an array element with a condition?
- MongoDB query to update an array using FindAndUpdate()?
- MongoDB query to push document into an array
- MongoDB query to update all documents matching specific IDs
- MongoDB query to match each element in a documents array to a condition?
- How to push an element into array in MongoDB?
- MongoDB query to find matching documents given an array with values?
- Removing an array element from MongoDB collection using update() and $pull
- Query an array of embedded documents in MongoDB and push another?
- Update Array element in MongoDB?
- Convert a field to an array using MongoDB update operation?
- MongoDB query to update array with another field?
- How to push an array in MongoDB?
- Convert a field to an array using update operation in MongoDB

Advertisements