Reputation: 656
My document:
{
"age":"20",
"name":"leandro"
}
I need prevent inserting new documents if another exists with same age and email. Can I do this using index?
Upvotes: 2
Views: 174
Reputation: 26012
Yes you can do it by creating index with unique=true as follows. After creating index, if you try to insert a document with same age & name then you will get duplicate key exception.
db.myObject.ensureIndex({age:1, name:1}, {unique : true})
For details you can read Create a Unique Index document.
Upvotes: 2