Mongo finds duplicates for records for two or more fields

I have such documents:

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

I want to find all documents that have duplicate values ​​for a pair of foreing and value .

+7
mongodb mongodb-query aggregation-framework
source share
2 answers

You can easily identify duplicates by performing 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 documents using foreign and value , and then create an array of _id values ​​for each of the grouped documents as a uniqueIds field using $addToSet . This gives you an array of unique expression values ​​for each group. Get the total number of grouped documents that will be used in subsequent stages of the pipeline, with the $sum operator.

In the second stage of the pipeline, use the $match operator to filter out all documents with a score of 1. Filtered documents are unique index keys.

The rest of the documents will be those from the collection that have duplicate key values ​​for the foreing and value pair.

+10
source share

We only need to group by 2 keys and select elements with a number greater than 1 to find duplicates.

Request : - It will be like

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

OUTPUT : - It will be like

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

Link Link: - How to find documents with the same field

+6
source share

All Articles