MongoDB shard key targeting questions

Hello, I hope you’re all doing well.

NOTE: the mongodb’s version in question is 3.0

We have a sharded setup of mongodb across several nodes.
Let’s say there is a collection events with documents like:

{
    user_id: <ObjectId>,
    company_id: <ObjectId>,
    event_id: <string>,
    start_date: <datetime>,
    end_date: <datetime>
}

The index used as a shard key is {company_id: 1, user_id: 1, event_id: 1}. Also, there is a multikey index on this collection {company_id: 1, user_id: 1, start_date: 1, end_date: 1}

Now, the query like {company_id: ...., user_id: ...., start_date: ...., end_date: ....} comes in. (NOTE: the query doesn’t have the event_id field).

My questions are:

  1. Would mongos be ALWAYS sending this query to all shards because it couldn’t figure out the exact shard without the event_id field?

  2. The same situation, but with the shard key by {company_id: 1, user_id: 1}. Will mongos be asking only a single shard for data without unnecessary requests to other shards?

Thank you.

Hello @Anton_Koval, welcome to the forum!

In the two questions you have mentioned, the query criteria use the Shard Key Prefix. Hence, both the queries will be Targeted Operations.

A targeted operation, uses a shard key (or its prefix, in case of a compound shard key) and accesses a specific shard or set of shards (but, not all the shards). As long as you are specifying any of the possible prefixes of the shard key in the query criteria, it will be a targeted operation.

Hi Anton,

Please note that MongoDB 3.0 reached end of life in February 2018, and no longer receives any security or maintenance updates. It’s definitely time to plan your upgrade to a supported version (currently MongoDB 3.6 or newer). There have been significant stability, performance, security, and feature improvements since the 3.0 release series.

Regards,
Stennie

Thank you. It makes sense.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.