I am newbie to mongodb. May I know how to avoid duplicate entries. In relational tables, we use primary key to avoid it. May I know how to specify it in Mongodb using java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use an index with the
{unique:true}option.You can also do this across multiple fields. See this section in the docs for more details and examples.
If you wish for
nullvalues to be ignored from the unique key, then you have to also make the index sparse (see here), by also adding thesparseoption:If you want to create the index using the MongoDB Java Driver. Try: