1

I have been playing around with MongoDB for a while and I have noticed that MongoDB and many NoSQL databases do not support unique key constraints as first class features.

MongoDB's _id field in each document is unique by default. However if I need to make another field unique in a document, the way to do is via uniqueIndexes.

What is the fundamental issue in supporting unique key constraints in NoSQL databases in general?

If there is a fundamental issue in using unique keys in NoSQL DBs, will there be an issue in creating unique indexes to achieve this requirement in MongoDB?

2
  • What is your question? Unique key constraints are provided by unique Indexes - like relational databases also do. Commented Jul 3, 2020 at 5:52
  • Ok I did not know that relational databases also use indexes to achieve this. So does it mean there is no issue in supporting unique keys in NoSQL databases? Is there any downside in this when the DB is distributed? Commented Jul 3, 2020 at 6:02

1 Answer 1

2

In MongoDB you can create unique constraints on both one field:

db.members.createIndex( { "user_id": 1 }, { unique: true } )

and multiple fields:

db.members.createIndex( { groupNumber: 1, lastname: 1, firstname: 1 }, { unique: true } )

You can create unique index on sharded clusters, too. It will be better to check the official docs as there are many examples (and limitations, too).

Let me know if you have any issues in creating unique constraint.

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.