A
A
AlexeyZernov2015-05-22 17:14:17
NoSQL
AlexeyZernov, 2015-05-22 17:14:17

MongoDB sampling on sharding, searching in parallel for all nodes?

Good day to all!
Interested in the question. For example, I implemented MongoDB auto-sharding for several nodes, let's say 10.
There is a "users" collection with 20 million documents that stores all users. They are spread over all nodes by id. And here it is necessary to register a new user with a unique username. We make a request:
db.posts.find({"username":"..."})
if it returns 0 records, then register with this username.
Here is the question, will MongoDB run through all the nodes and records in turn or in parallel? It's just that this solution doesn't seem to be as productive.
The same example can be given with post comments, the comments are spread over the nodes, and in order to display them all MongoDB searches for them among all the nodes and displays them?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Vadim Kharitonov, 2015-05-22
@virtuozzz

Why do find? You may have a situation where two users with the same name can register at the same time. And it turns out that find will return false, but when inserting data, someone will see an error. It's better to index on the username field and just insert the data. If the data is inserted, then ok, if not, then the username is invalid. It turns out that you will always run one request, instead of two.
On the topic:
a search by a unique index will immediately get to the desired shard without any additional scans.

P
Puma Thailand, 2015-05-22
@opium

In parallel, of course.
If it ran in turn, there would be no sense in the shard at all.

L
lega, 2015-05-22
@lega

They are spread over all nodes by id

Why by id?
Make a unique index (sharding) on ​​username, then the request will be to a specific shard in which this name (is or should be). And when adding, it will not be necessary to check, because the duplicate of the name will not be recorded (there will be an error).

Z
ZOXEXIVO, 2017-04-22
@ZOXEXIVO

Mongos.exe analyzes the request and sees if there is a call to the shard-key inside it. If it is there, then the request goes to a specific shard, otherwise, requests are sent to all shards at the same time and aggregated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question