MongoDB-通过多个键查找重复的文档 [英] MongoDB -- Find duplicate documents by multiple keys

查看:240
本文介绍了MongoDB-通过多个键查找重复的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
}

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

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 } ]);

推荐答案

是像通过多个键进行分组一样简单,还是我误解了您的问题?

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 } } },
...

这篇关于MongoDB-通过多个键查找重复的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆