5

I have a collection with documents that look like the following:

{
        "_id" : ObjectId("55b377cb66b393427367c3e2"),
        "comment" : "This is a comment",
        "url_key" : "55b377cb66b393427367c3df", //This is an ObjectId from another record in a different collection
}

I need to find records in this collection that contain duplicate values for the both the comment AND the url_key.

I can easily generate (using aggregate) duplicate records for the same, single, key (eg: comment), but I can't figure out how to group by/aggregate for multiple keys.

Here's my current aggregation pipeline:

db.comments.aggregate([ { $group: { _id: { comment: "$comment" }, uniqueIds: { $addToSet: "$_id" }, count: { $sum: 1 } } }, { $match: { count: { $gte: 2 } } }, { $sort: { count : -1} }, {$limit 10 } ]);
DAXaholic
  • 33,312
  • 6
  • 76
  • 74
gleb1783
  • 451
  • 1
  • 6
  • 18
  • Possible duplicate of [Find all duplicate documents in a MongoDB collection by a key field](http://stackoverflow.com/questions/9491920/find-all-duplicate-documents-in-a-mongodb-collection-by-a-key-field) – DhruvPathak Sep 14 '16 at 12:54

1 Answers1

8

Is it as simple as grouping by multiple keys or did I misunderstand your question?

...
{ $group: { _id: { id: "$_id", comment: "$comment" }, count: { $sum: 1 } } },
{ $match: { count: { $gte: 2 } } },
...
DAXaholic
  • 33,312
  • 6
  • 76
  • 74