MongoDB Duplicate Documents Despite Unique Key
In MongoDB, creating a unique index on multiple fields should prevent duplicate documents with the same values for those fields. However, in some cases, duplicate documents may still be inserted even after adding an index.
Problem:
A unique index was created on the "uid" and "sid" fields in a collection, but documents with duplicate values for these fields were still being inserted using the PHP driver.
Solution:
MongoDB Shell
Check for Existing Duplicates:
db.user_services.aggregate([ { "$group": { "_id": { "uid": "$uid", "sid": "$sid" }, "dups": { "$push": "$_id" }, "count": { "$sum": 1 } }}, { "$match": { "count": { "$gt": 1 } }} ])
Remove Duplicates:
db.user_services.remove({ "_id": {"$in": doc.dups }});
Create Unique Index:
db.user_services.createIndex({"uid":1 , "sid": 1},{unique:true})
PHP
Use Update with Upsert:
<code class="php">$collection->update( array( "uid" => 1, "sid" => 1 ), array( '$set' => $someData ), array( 'upsert' => true ) );</code>
Handle Multiple Updates:
<code class="php">$collection->update( array( "uid" => 1, "sid" => 1 ), array( '$set' => array( "field" => "this" ), '$inc' => array( "counter" => 1 ), '$setOnInsert' => array( "newField" => "another" ) ), array( "upsert" => true ) );</code>
The above is the detailed content of Why Am I Still Seeing Duplicate Documents in MongoDB Despite Using a Unique Index?. For more information, please follow other related articles on the PHP Chinese website!