在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 with count> 1

db.collection.aggregate(
    {"$group" : { "_id": "$name", "count": { "$sum": 1 } } },
    {"$match": {"_id" :{ "$ne" : null } , "count" : {"$gt": 1} } }, 
    {"$project": {"name" : "$_id", "_id" : 0} }
)