1

How can I check if data in a collection exists. I need to ensure that usernames are unqiue. Any ideas?

1 Answer 1

1

The easiest way is to just put a unique index on the data that you need to be unique. If you try and insert new data with the same value, Mongo will return an error. At that point you can handle the error however you want.

You can read more about unique indexes at http://www.mongodb.org/display/DOCS/Indexes#Indexes-unique%3Atrue. You create them like any other index using the Node driver.

// everyone's username must be unique:
db.createIndex(userCollection, {username:1}, {unique:true});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.