Reputation: 9974
I have the following index on my collection:
{
"v" : 1,
"key" : {
"story_feed_id" : 1,
"story_guid" : 1
},
"unique" : true,
"ns" : "newsblur.stories",
"name" : "story_feed_id_1_story_guid_1",
"background" : false,
"dropDups" : false
},
and want it update the dropDups to be true
what is the best way to do this?
Upvotes: 0
Views: 558
Reputation: 1500
if you can't drop just remove the duplicates with the next code (Node.js)
var MongoClient = require('mongodb').MongoClient
, assert = require('assert');
// Connection URL
var url = 'mongodb://localhost:27017/videossite';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected correctly to server");
var collection = db.collection("video");
collection.aggregate([
{ $group: {
// Group by fields to match on (a,b)
_id: { videoId: "$videoId" },
// Count number of matching docs for the group
count: { $sum: 1 },
// Save the _id for matching docs
docs: { $push: "$_id" }
}},
// Limit results to duplicates (more than 1 match)
{ $match: {
count: { $gt : 1 }
}}
],function(err,result){
for(var i = 0 ; i < result.length ; i++){
collection.remove({videoId:result[i]._id.videoId},function(err, result) {
console.log("result",result.result.n)
});
}
});
});
Upvotes: 0
Reputation: 18595
There is no valid use case where you would need to enable that flag. The createIndex/ensureIndex invocation that would result in the index definition in your post would fail if duplicates on the index keys were present in that collection :
> db.test.save({a:1})
> db.test.save({a:1})
> db.test.ensureIndex({a:1}, {unique:true})
E11000 duplicate key error index: test.test.$a_1 dup key: { : 1.0 }
In other words, there does not exist a situation where you need to enable that flag retroactively. The only reason that flag exists is to drop duplicates upon createIndex invocations for collections that have duplicate index key values and you want to drop them rather than the call failing with the error above.
Upvotes: 3