MongoDB-通过多个键查找重复的文档

gle*_*783 3 mongodb mongodb-query aggregation-framework mongodb-aggregation

我有一个包含如下文档的集合:

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

我需要在此集合中找到包含注释和url_key重复值的记录。

我可以轻松地生成(使用汇总)相同,单个键(例如:注释)的重复记录,但是我不知道如何对多个关键字进行分组/汇总。

这是我当前的聚合管道:

db.comments.aggregate([ { $group: { _id: { comment: "$comment" }, uniqueIds: { $addToSet: "$_id" }, count: { $sum: 1 } } }, { $match: { count: { $gte: 2 } } }, { $sort: { count : -1} }, {$limit 10 } ]);
Run Code Online (Sandbox Code Playgroud)

DAX*_*lic 5

它像按多个键分组一样简单,还是我误解了您的问题?

...
{ $group: { _id: { id: "$_id", comment: "$comment" }, count: { $sum: 1 } } },
{ $match: { count: { $gte: 2 } } },
...
Run Code Online (Sandbox Code Playgroud)