- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the duplicate values of a field in MongoDB?
Use aggregate() method to get the duplicate value of a field. Let us first create a collection with documents using the following query
> db.findAllNonDistinctDemo.insertOne({"UserName":"John","UserAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry","UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry","UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David","UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John","UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert","UserAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c9950b1863d6ffd454bb64d") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Mike","UserAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5c9950bc863d6ffd454bb64e") }
Following is the query to display all documents from a collection with the help of find() method
> db.findAllNonDistinctDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c995078863d6ffd454bb647"), "UserName" : "John", "UserAge" : 28 } { "_id" : ObjectId("5c995081863d6ffd454bb648"), "UserName" : "Larry", "UserAge" : 21 } { "_id" : ObjectId("5c995089863d6ffd454bb649"), "UserName" : "Larry", "UserAge" : 23 } { "_id" : ObjectId("5c995093863d6ffd454bb64a"), "UserName" : "David", "UserAge" : 22 } { "_id" : ObjectId("5c99509d863d6ffd454bb64b"), "UserName" : "John", "UserAge" : 26 } { "_id" : ObjectId("5c9950a7863d6ffd454bb64c"), "UserName" : "Robert", "UserAge" : 24 } { "_id" : ObjectId("5c9950b1863d6ffd454bb64d"), "UserName" : "Robert", "UserAge" : 25 } { "_id" : ObjectId("5c9950bc863d6ffd454bb64e"), "UserName" : "Mike", "UserAge" : 29 }
Following is the query to find all the duplicate values of a field in MongoDB
> db.findAllNonDistinctDemo.aggregate([ ... { "$group": { ... "_id": "$UserName", ... "Counter": { "$sum": 1 } ... }}, ... { "$match": { ... "Counter": { "$gt": 1 } ... }} ... ]).pretty();
This will produce the following output. All the duplicate records are visible now
{ "_id" : "Robert", "Counter" : 2 } { "_id" : "Larry", "Counter" : 2 } { "_id" : "John", "Counter" : 2 }
- Related Articles
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- Group with multiple fields and get the count of duplicate field values grouped together in MongoDB
- How to get distinct list of sub-document field values in MongoDB?
- Sum the score of duplicate column values in MongoDB documents?
- List all values of a certain field in MongoDB?
- Limit number of values in a field using MongoDB?
- Limit number of values in a field with MongoDB?
- Find all the non-distinct values of a field in MongoDB?
- MongoDB query to limit the returning values of a field?
- Find all duplicate documents in a MongoDB collection by a key field?
- MongoDB transaction & indexes for duplicate values
- Get the count of duplicate values from a single column in MySQL?
- Fetch specific field values in MongoDB
- How to remove duplicate values inside a list in MongoDB?
- Update all the values of a field with a specific string in MongoDB?

Advertisements