I have a collection that has a username as a field. The Model defines this field to be unique. However I was able to insert a duplicate value in the database.
class Profile
include Mongoid::Document
include Mongoid::Paperclip
field :username
index({ username: 1 } , { unique: true })
end
The Collection however has 2 usernames that are the same
{ "_id" : ObjectId( "50b3b323421aa95da6000004" ),
"username" : "marceloreuse" }
{ "_id" : ObjectId( "50b3b567421aa93d84000002" ),
"username" : "marceloreuse" }
What went wrong here?
I would double check your indexes - from the console try db.collection.getIndexes()
and make sure your index is present.
In case you missed it, Mongoid doesn't auto build the index because you specified it - you need to run the included: rake db:mongoid:create_indexes
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With