
- 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 key value where another key equals some value in MongoDB?
Use $elemMatch fir this with $set
Let us first create a collection with documents −
> dbkeyValueDemoinsertOne( { "_id" : new ObjectId(), "CustomerDetails" : [ { "Name" : "Chris", "Age" :24, }, { "Name" : "Robert", "Age" :29, }, { "Name" : "David", "Age" :35, } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefcf36ef71edecf6a1f6bf") }
Following is the query to display all documents from a collection with the help of find() method −
> dbkeyValueDemofind()pretty();
Output
{ "_id" : ObjectId("5cefcf36ef71edecf6a1f6bf"), "CustomerDetails" : [ { "Name" : "Chris", "Age" : 24 }, { "Name" : "Robert", "Age" : 29 }, { "Name" : "David", "Age" : 35 } ] }
Here is the query to update key value where different key equals some value −
> dbkeyValueDemoupdate( {"CustomerDetails":{"$elemMatch":{"Name":"David"}}}, {"$set":{"CustomerDetails$Age":56}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 } )
Let us check the document once again −
> dbkeyValueDemofind()pretty();
Output
{ "_id" : ObjectId("5cefcf36ef71edecf6a1f6bf"), "CustomerDetails" : [ { "Name" : "Chris", "Age" : 24 }, { "Name" : "Robert", "Age" : 29 }, { "Name" : "David", "Age" : 56 } ] }
- Related Articles
- Update key value where different key equals some value in MongoDB?
- How to update value of a key in a list of a json in MongoDB?
- Java Program to Update value of HashMap using key
- C++ Program to Update value of Dictionary using key
- Update MongoDB field using value of another field?
- MongoDB: Using reference as key and manually adding a value?
- How to update the value of a key in a dictionary in Python?
- Update object in array with a specific key in MongoDB
- Get key from value in JavaScript
- How to retrieve a value from MongoDB by its key name?
- JavaScript - Sort key value pair object based on value?
- Time Based Key-Value Store in C++
- Add key-value pair in C# Dictionary
- Accessing Key-value in a Python Dictionary
- How to access subdocument value when the key is a number in MongoDB?

Advertisements