
- 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
Find maximum score for duplicate Name values in MongoDB?
To find maximum score, use GROUP() to group documents in a collection. Let us create a collection with documents −
> db.demo114.insertOne({"Score":60,"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc57d8f64a552dae6354") } > db.demo114.insertOne({"Score":87,"Nam+e":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc5ad8f64a552dae6355") } > db.demo114.insertOne({"Score":45,"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc5dd8f64a552dae6356") } > db.demo114.insertOne({"Score":67,"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc68d8f64a552dae6357") } > db.demo114.insertOne({"Score":38,"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2efc74d8f64a552dae6358") }
Display all documents from a collection with the help of find() method −
> db.demo114.find();
This will produce the following output −
{ "_id" : ObjectId("5e2efc57d8f64a552dae6354"), "Score" : 60, "Name" : "Chris" } { "_id" : ObjectId("5e2efc5ad8f64a552dae6355"), "Score" : 87, "Name" : "Chris" } { "_id" : ObjectId("5e2efc5dd8f64a552dae6356"), "Score" : 45, "Name" : "David" } { "_id" : ObjectId("5e2efc68d8f64a552dae6357"), "Score" : 67, "Name" : "Chris" } { "_id" : ObjectId("5e2efc74d8f64a552dae6358"), "Score" : 38, "Name" : "David" }
Following is the query to find maximum. This calculates the maximum score of let’s say duplicate name values, such as “Chris” and “David” −
> db.demo114.group( ... {key: {Name:true}, ... reduce: function(c,p) { ... if (p.maximumScore < c.Score) { ... p.maximumScore = c.Score; ... } ... }, ... initial: { maximumScore: 0 }} ... );
This will produce the following output −
[ { "Name" : "Chris", "maximumScore" : 87 }, { "Name" : "David", "maximumScore" : 45 } ]
- Related Articles
- Sum the score of duplicate column values in MongoDB documents?
- MongoDB transaction & indexes for duplicate values
- Find duplicate records in MongoDB?
- Get the duplicate values of a field in MongoDB?
- MongoDB aggregation group and remove duplicate array values?
- Program to find maximum score in stone game in Python
- How to remove duplicate values inside a list in MongoDB?
- Program to find maximum score from removing stones in Python
- Find the MongoDB collection size for name “Chris”
- Program to find maximum additive score by deleting numbers in Python
- Program to find maximum score from performing multiplication operations in Python
- Program to find maximum score of brick removal game in Python
- Program to find maximum score of a good subarray in Python
- C++ Program to find maximum score of bit removal game
- Find keys with duplicate values in dictionary in Python

Advertisements