
- 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 MongoDB document with array containing the maximum occurrence of a specific value
For this, you can use aggregate(). Let us first create a collection with documents −
> db.countOccurrencesDemo.insertOne({"ListOfValues":[65,87,89,65,67,87,87,87]}); { "acknowledged" : true, "insertedId" : ObjectId("5e06ef9325ddae1f53b621eb") } > db.countOccurrencesDemo.insertOne({"ListOfValues":[102,65,87,65,89,65,89,65,89,65]}); { "acknowledged" : true, "insertedId" : ObjectId("5e06efaa25ddae1f53b621ec") }
Following is the query to display all documents from a collection with the help of find() method −
> db.countOccurrencesDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5e06ef9325ddae1f53b621eb"), "ListOfValues" : [ 65, 87, 89, 65, 67, 87, 87, 87 ] } { "_id" : ObjectId("5e06efaa25ddae1f53b621ec"), "ListOfValues" : [ 102, 65, 87, 65, 89, 65, 89, 65, 89, 65 ] }
Here is the query to find document with array containing the maximum occurrence of a specific value −
> db.countOccurrencesDemo.aggregate( ... [ ... { "$project": { ... "ListOfValues": 1, ... "OccurencesValue": { ... "$size": { ... "$filter": { ... "input": "$ListOfValues", ... "as": "v", ... "cond": { "$eq": [ "$$v", 65] } ... } ... } ... } ... }}, ... { "$group": { ... "_id": "$OccurencesValue", ... "MyValues": { "$push": "$$ROOT" } ... }}, ... { "$sort": { "_id": -1 } }, ... { "$limit": 1 } ... ] ... );
This will produce the following output −
{ "_id" : 5, "MyValues" : [ { "_id" : ObjectId("5e06efaa25ddae1f53b621ec"), "ListOfValues" : [ 102, 65, 87, 65, 89, 65, 89, 65, 89, 65 ], "OccurencesValue" : 5 } ] }
- Related Articles
- Find document with array that contains a specific value in MongoDB
- Find the document by field name with a specific value in MongoDB?
- Find documents with arrays not containing a document with a particular field value in MongoDB?
- Fetch a specific document in MongoDB with array elements
- Update only a specific value in a MongoDB document
- Update a specific MongoDB document in array with $set and positional $ operator?
- How to find specific array elements in MongoDB document with query and filter with range?
- Extract a MongoDB document with a specific string
- MongoDB query to pull a specific value from a document
- Find which MongoDB document contains a specific string?
- How to get a specific object from array of objects inside specific MongoDB document?
- Find MongoDB documents where elements of an array have a specific value?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Find the MongoDB document from sub array?
- Accessing array values in a MongoDB collection to fetch a specific document

Advertisements