wazzaday
wazzaday

Reputation: 9664

MongoDB Ensure Index not stopping Duplicates

I am trying to stop duplicates in my Mongo DB Collection but they are still getting in. I am reading data from twitter and storing it like:

var data = {
    user_name: response[i].user.screen_name,
    profile_image: response[i].user.profile_image_url,
    content: {
        text: response[i].text
    },
    id: response[i].id_str,
};

and I have the following to stop any duplicates:

db[collection].ensureIndex( { id: 1, "content.text": 1 }, { unique: true, dropDups: true } );

The id field is working and no duplicates appear but "content.text" field does not work and duplicates are appearing. Any Ideas why?

Upvotes: 1

Views: 268

Answers (1)

BatScream
BatScream

Reputation: 19700

When you enforce a unique constraint on a composite index, two documents are considered same only if the documents have the same value for both id and context.text fields and not for either key individually.

To enforce unique constraints on the fields, id and context.text individually, You could enforce it as below:

db.col.ensureIndex({"id":1},{unique:true}) and similarly for the other field.

Upvotes: 2

Related Questions