Home  >  Article  >  Backend Development  >  Why Am I Getting Duplicate Documents in My MongoDB Collection Even After Creating a Unique Key with `dropDups`?

Why Am I Getting Duplicate Documents in My MongoDB Collection Even After Creating a Unique Key with `dropDups`?

DDD
DDDOriginal
2024-11-04 07:10:311007browse

Why Am I Getting Duplicate Documents in My MongoDB Collection Even After Creating a Unique Key with `dropDups`?

MongoDB Duplicate Documents After Adding Unique Key

Problem Statement

Despite creating a collection with a unique key on the "uid" and "sid" fields, duplicate documents are still being inserted into the collection.

Technical Details

The index creation command used:

db.user_services.createIndex({"uid":1 , "sid": 1},{unique:true,dropDups: true})

Cause

The index creation fails due to the presence of duplicate documents in the collection. In MongoDB version 3.0.0, the "dropDups" option is not functioning correctly, resulting in this issue.

Solution

  • Remove duplicate documents: To resolve this, remove the duplicate documents before creating the unique index. This can be achieved using aggregation and removal queries.
db.events.aggregate([
    { "$group": {
        "_id": { "uid": "$uid", "sid": "$sid" },
        "dups": { "$push": "$_id" },
        "count": { "$sum": 1 }
    }},
    { "$match": { "count": { "$gt": 1 } }}
]).forEach(function(doc) {
    doc.dups.shift();
    db.events.remove({ "_id": {"$in": doc.dups }});
});
  • Create the unique index: After removing the duplicates, create the unique index as intended.
db.events.createIndex({"uid":1 , "sid": 1},{unique:true})

Upsert Operation

For the second part of the question, to achieve the desired behavior of inserting or updating a document based on the presence of existing data, use the ".update()" method with the "upsert" option:

$collection->update(
    array( "uid" => 1, "sid" => 1 ),
    array( '$set' => $someData ),
    array( 'upsert' => true )
);

This will modify found documents and insert new documents as needed. Additionally, you can use "$setOnInsert" to specify fields that should only be set during document insertion.

The above is the detailed content of Why Am I Getting Duplicate Documents in My MongoDB Collection Even After Creating a Unique Key with `dropDups`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn