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
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 }}); });
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!