Q
Q
quest20172017-05-26 15:24:15
MongoDB
quest2017, 2017-05-26 15:24:15

Does mongodb support unique keys?

Here one of the mongodb developers claims that mongodb does not support the uniqueness of multiple keys within all shards and that if you create non-unique keys everything will be very bad - https://www.youtube.com/watch?v=3Yl4hrMb9WA&featur...
Question: how is it possible to write non-unique keys to mongodb if we have uniqueness within a shard? we don’t choose which shard to write to, do we? does he talk about some custom keys or about the possible non-uniqueness of ObjectId in the process of resharding?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RidgeA, 2017-05-26
@RidgeA

https://docs.mongodb.com/manual/core/sharding-shar...
1. For a collection that is divided into shards, _id, shard key and a composite index will be unique within the entire collection, in which shard key is a prefix ( the first value in the composite index)
2. Create a unique index, if the collection is sharded, it will not work.
3. Sharing the collection, if there is a unique index - will not work.
I'm not sure, but I assume that this is due to the fact that in order to insert a new entry into the sharded collection with a unique key, you will have to access each shard and find out what values ​​\u200b\u200bfor this field in each shard. And it can be expensive. As for the shard key and _id, they are most likely processed by mongos before sending a request to add to a specific shard - this is why they are guaranteed to be unique.
For a collection that is not sharded, unique keys are supported.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question