Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo find duplicates for entries for two or more fields

I have documents like this:

{     "_id" : ObjectId("557eaf444ba222d545c3dffc"),     "foreing" : ObjectId("538726124ba2222c0c0248ae"),     "value" : "test", } 

I want to find all documents which have duplicated values for pair foreing & value.

like image 266
Haris Hajdarevic Avatar asked Feb 25 '16 10:02

Haris Hajdarevic


People also ask

How do I search for multiple fields in MongoDB?

MongoDB provides the find() that is used to find multiple values or documents from the collection. The find() method returns a cursor of the result set and prints all the documents. To find the multiple values, we can use the aggregation operations that are provided by MongoDB itself.


2 Answers

You can easily identify the duplicates by running the following aggregation pipeline operation:

db.collection.aggregate([     {          "$group": {              "_id": { "foreing": "$foreing", "value": "$value" },              "uniqueIds": { "$addToSet": "$_id" },             "count": { "$sum": 1 }          }     },      { "$match": { "count": { "$gt": 1 } } } ]) 

The $group operator in the first step is used to group the documents by the foreign and value key values and then create an array of _id values for each of the grouped documents as the uniqueIds field using the $addToSet operator. This gives you an array of unique expression values for each group. Get the total number of grouped documents to use in the later pipeline stages with the $sum operator.

In the second pipeline stage, use the $match operator to filter out all documents with a count of 1. The filtered-out documents represent unique index keys.

The remaining documents will be those in the collection that have duplicate key values for pair foreing & value.

like image 93
chridam Avatar answered Sep 21 '22 19:09

chridam


We only have to group on the bases of 2 keys, and select the elements with count greater than 1, to find the duplicates.

Query :- Will be like

db.mycollection.aggregate(     { $group: {          _id: { foreing: "$foreing", value: "$value" },         count: { $sum:  1 },         docs: { $push: "$_id" }     }},     { $match: {         count: { $gt : 1 }     }} ) 

OUTPUT :- Will be like

{     "result" : [         {             "_id" : {                 "foreing" : 1,                 "value" : 2             },             "count" : 2,             "docs" : [                 ObjectId("34567887654345678987"),                 ObjectId("34567887654345678987")             ]         }     ],     "ok" : 1 } 

Reference Link :- How to find mongo documents with a same field

like image 27
Nishant Avatar answered Sep 18 '22 19:09

Nishant