
- 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 array in MongoDB document by variable index?
To update array in MongoDB document by variable index, use the below syntax. Here, yourIndexValue in the index value, where yourIndexVariableName is the variable name for index −
var yourIndexVariableName= yourIndexValue, anyVariableName= { "$set": {} }; yourVariableName["$set"]["yourFieldName."+yourIndexVariableName] = "yourValue"; db.yourCollectionName.update({ "_id": yourObjectId}, yourVariableName);
Let us first create a collection with documents −
> db.updateByVariableDemo.insertOne({"StudentSubjects":["MySQL","Java","SQL Server","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd553c37924bb85b3f4893a") }
Following is the query to display all documents from a collection with the help of find() method −
> db.updateByVariableDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd553c37924bb85b3f4893a"), "StudentSubjects" : [ "MySQL", "Java", "SQL Server", "PL/SQL" ] }
Following is the query to update array in MongoDB document by variable index −
> var indexValue = 1, ... valueToUpdate= { "$set": {} }; > valueToUpdate["$set"]["StudentSubjects."+indexValue] = "MongoDB"; MongoDB > db.updateByVariableDemo.update({ "_id": ObjectId("5cd553c37924bb85b3f4893a") }, valueToUpdate) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Let us display the documents once again −
> db.updateByVariableDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd553c37924bb85b3f4893a"), "StudentSubjects" : [ "MySQL", "MongoDB", "SQL Server", "PL/SQL" ] }
- Related Articles
- Update object at specific Array Index in MongoDB?
- MongoDB query to update array object in index N?
- Update MongoDB variable value with variable itself?
- Update only a single document in MongoDB
- Update a specific MongoDB document in array with $set and positional $ operator?
- MongoDB query to update nested document
- MongoDB query on nth element (variable index) of subdocument array
- MongoDB findOneAndUpdate() to update a single document
- MongoDB query to update the nested document?
- Update only a specific value in a MongoDB document
- Update two separate arrays in a document with one update call in MongoDB?
- Update Array element in MongoDB?
- How do you update a MongoDB document while replacing the entire document?
- How to update a MongoDB document for adding a new item to an array?
- Update a MongoDB document with Student Id and Name

Advertisements