
- 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 replace value in an array?
Use $set to replace value in an array. Let us first create a collection with documents −
> db.replaceValueInArrayDemo.insertOne({"StudentScores":[45,56,78]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0421a844af18acdffb7") } > db.replaceValueInArrayDemo.insertOne({"StudentScores":[33,90,67]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd7f0521a844af18acdffb8") }
Following is the query to display all documents from a collection with the help of find() method −
> db.replaceValueInArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7f0421a844af18acdffb7"), "StudentScores" : [ 45, 56, 78 ] } { "_id" : ObjectId("5cd7f0521a844af18acdffb8"), "StudentScores" : [ 33, 90, 67 ] }
Following is the query to replace value in array −
> db.replaceValueInArrayDemo.update({_id: ObjectId("5cd7f0421a844af18acdffb7"), StudentScores:45}, {$set: {'StudentScores.$': 99}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us check the document once again −
> db.replaceValueInArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd7f0421a844af18acdffb7"), "StudentScores" : [ 99, 56, 78 ] } { "_id" : ObjectId("5cd7f0521a844af18acdffb8"), "StudentScores" : [ 33, 90, 67 ] }
- Related Articles
- MongoDB query to replace value with aggregation?
- Replace an array field value with MongoDB?
- Query an array in MongoDB to fetch a specific value
- Which MongoDB query finds same value multiple times in an array?
- MongoDB query to access an object in an array
- Make MongoDB replace single array value with string?
- MongoDB query check if value in array property?
- MongoDB query to test if a value is in array?
- What kind of MongoDB query finds same value multiple times in an array?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB query to push document into an array
- MongoDB query to update an array using FindAndUpdate()?
- Query to retrieve multiple items in an array in MongoDB?
- MongoDB query for Partial Object in an array
- MongoDB query to remove empty objects in an object-array?

Advertisements