Find all duplicate documents in a MongoDB collection by a key field

后端 未结 5 487
温柔的废话
温柔的废话 2020-11-28 22:00

Suppose I have a collection with some set of documents. something like this.

{ \"_id\" : ObjectId(\"4f127fa55e7242718200002d\"), \"id\":1, \"name\" : \"foo\"         


        
5条回答
  •  一个人的身影
    2020-11-28 22:28

    aggregation pipeline framework can be used to easily identify documents with duplicate key values:

    // Desired unique index: 
    // db.collection.ensureIndex({ firstField: 1, secondField: 1 }, { unique: true})
    
    db.collection.aggregate([
      { $group: { 
        _id: { firstField: "$firstField", secondField: "$secondField" }, 
        uniqueIds: { $addToSet: "$_id" },
        count: { $sum: 1 } 
      }}, 
      { $match: { 
        count: { $gt: 1 } 
      }}
    ])
    

    ~ Ref: useful information on an official mongo lab blog:

    https://blog.mlab.com/2014/03/finding-duplicate-keys-with-the-mongodb-aggregation-framework

提交回复
热议问题