Mongoose Unique index not working!


Question

I'm trying to let MongoDB detect a duplicate value based on its index. I think this is possible in MongoDB, but through the Mongoose wrapper things appear to be broken. So for something like this:

User = new Schema ({
  email: {type: String, index: {unique: true, dropDups: true}}
})

I can save 2 users with the same email. Darn.

The same issue has been expressed here: https://github.com/LearnBoost/mongoose/issues/56, but that thread is old and lead to nowhere.

For now, I'm manually making a call to the db to find the user. That call is not expensive since "email" is indexed. But it would still be nice to let it be handled natively.

Does anyone have a solution to this?

1
72
4/4/2011 7:18:49 AM

Accepted Answer

Oops! You just have to restart mongo.

77
4/4/2011 7:22:07 AM

Oops! You just have to restart mongo.

And re-index too !

In testing, I just did a:

mongo <db-name>
> db.dropDatabase()

Though if you have important data in there, you probably just need to:

mongo <db-name>
> db.<collection-name>.reIndex()

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon