When creating a collection in MongoDB and adding a unique index using createIndex({"uid":1 , "sid": 1},{unique:true,dropDups: true}), it may appear that duplicate documents are still being inserted despite the unique key constraint.
Upon examining the index creation, an error message indicating "duplicate key error" is likely encountered. This is because MongoDB 3.0.0 had a bug that prevented the index from being created when duplicates existed.
To resolve this, first remove duplicate records using an aggregate query and the $match operator.
<code class="javascript">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 }}); });</code>
Once duplicates are removed, the unique index can be created without encountering an error.
To insert documents that don't already exist or update existing documents, use the update() method with the upsert option set to true.
<code class="php">$collection->update( array( "uid" => 1, "sid" => 1 ), array( '$set' => $someData ), array( 'upsert' => true ) );</code>
This will insert the document if it does not exist, otherwise it will update the document by setting the fields specified in the $set array.
The above is the detailed content of Why Am I Still Seeing Duplicate Documents in MongoDB Despite Having a Unique Index?. For more information, please follow other related articles on the PHP Chinese website!