gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:26:03 25 4
gpt4 key购买 nike

我有一个文档集合,如下所示:

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

我需要在此集合中查找包含评论和 url_key 的重复值的记录。

我可以轻松地为相同的单个键(例如:评论)生成(使用聚合)重复记录,但我不知道如何对多个键进行分组/聚合。

这是我当前的聚合管道:

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

最佳答案

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

...
{ $group: { _id: { id: "$_id", comment: "$comment" }, count: { $sum: 1 } } },
{ $match: { count: { $gte: 2 } } },
...

关于MongoDB——通过多个键查找重复文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39490816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com