Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB -- Find duplicate documents by multiple keys

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 } ]);
like image 658
gleb1783 Avatar asked Sep 14 '16 12:09

gleb1783


1 Answers

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 } } },
...
like image 82
DAXaholic Avatar answered Oct 14 '22 16:10

DAXaholic