在 MongoDB 中查找重复记录 [英] Find duplicate records in MongoDB
本文介绍了在 MongoDB 中查找重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 mongo 集合中找到重复的字段.
How would I find duplicate fields in a mongo collection.
我想检查是否有任何名称"字段重复.
I'd like to check if any of the "name" fields are duplicates.
{
"name" : "ksqn291",
"__v" : 0,
"_id" : ObjectId("540f346c3e7fc1054ffa7086"),
"channel" : "Sales"
}
非常感谢!
推荐答案
在 name
上使用聚合并得到 name
with count >1
:
Use aggregation on name
and get name
with count > 1
:
db.collection.aggregate([
{"$group" : { "_id": "$name", "count": { "$sum": 1 } } },
{"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } },
{"$project": {"name" : "$_id", "_id" : 0} }
]);
按照重复次数从多到少对结果进行排序:
To sort the results by most to least duplicates:
db.collection.aggregate([
{"$group" : { "_id": "$name", "count": { "$sum": 1 } } },
{"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } },
{"$sort": {"count" : -1} },
{"$project": {"name" : "$_id", "_id" : 0} }
]);
要与name"以外的其他列名称一起使用,请更改$name";到$column_name"
To use with another column name than "name", change "$name" to "$column_name"
这篇关于在 MongoDB 中查找重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文