
- 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 different key equals some value in MongoDB?
Let us create a collection with documents −
> db.demo196.insertOne( ... { ... ... "Id" : "101", ... "details" : [ ... { ... "FirstName" : "Chris", ... "LastName" : "Brown", ... "Score" : 45 ... }, ... { ... "FirstName" : "David", ... "LastName" : "Miller", ... "Score" : 87 ... }, ... { ... "FirstName" : "John", ... "LastName" : "Doe", ... "Score" : 56 ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3af6b103d395bdc21346d7") }
Display all documents from a collection with the help of find() method −
> db.demo196.find();
This will produce the following output −
{ "_id" : ObjectId("5e3af6b103d395bdc21346d7"), "Id" : "101", "details" : [ { "FirstName" : "Chris", "LastName" : "Brown", "Score" : 45 }, { "FirstName" : "David", "LastName" : "Miller", "Score" : 87 }, { "FirstName" : "John", "LastName" : "Doe", "Score" : 56 } ] }
Following is the query to update key value where different key equals some value −
> db.demo196.update({"details":{"$elemMatch":{"FirstName" : "David", "LastName" : "Miller"}}}, ... {"$set":{"details.$.Score":98}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo196.find();
This will produce the following output −
{ "_id" : ObjectId("5e3af6b103d395bdc21346d7"), "Id" : "101", "details" : [ { "FirstName" : "Chris", "LastName" : "Brown", "Score" : 45 }, { "FirstName" : "David", "LastName" : "Miller", "Score" : 98 }, { "FirstName" : "John", "LastName" : "Doe", "Score" : 56 } ] }
- Related Articles
- Update key value where another 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
- Java Program to replace key and value in HashMap with identical key and different values
- 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