butterguns
butterguns

Reputation: 37

Query against local MongoDB shard data only

I have a sharded collection, with a shard key "user id". I would like to perform a query where, instead of passing the shard key, I simply restrict the query to only the data on the local mongos shard.

Is this possible / advisable?

Furthermore, can it be used with findAndModify? This would allow me to perform atomic updates on local documents, without specifying a shard key in the query.

Edit As stated in some answers and comments below, my understanding of mongos vs. mongod was a little skewed. I now appreciate that mongos doesn't hold the local data.

Upvotes: 0

Views: 1292

Answers (3)

Stennie
Stennie

Reputation: 65323

Does mongos have any "local" data?

No. Each mongos daemon routes queries to your shards and does not store any data itself, so there is no such concept as "local" documents stored by a mongos. The mongos interface provides a logical view of the entire sharded cluster and does not have affinity to a specific shard.

Based on the type of query/command you send to mongos, the query will be:

  • Directed: sent to a specific shard if the query uses the shard key
  • Targeted: sent to applicable shards if the query includes multiple shard key values (or uses a prefix subset of a compound shard key)
  • Scatter/gather: sent to all shards, if the query is not using the shard key

Should I read from shards directly?

No. It's technically possible to read data from the shards directly but definitely not recommendable as you can get an inconsistent view of data. For example, if there is a migration in progress the data will temporarily exist on both the donor shard and the target shard. Similarly, copies of documents may be orphaned as the result of failed migrations.

A query through mongos correctly directs queries to the appropriate shard(s) and filters results based on the sharded cluster metadata.

Can I use findAndModify() on a sharded collection without a query based on a shard key?

No. For a sharded collection, findAndModify() requires a query based on the shard key. The shard key provides a guarantee that the requested document only exists on one shard.

Can I update sharded collections without going through mongos?

No. All updates to a sharded collection must go through mongos.

Upvotes: 1

Sammaye
Sammaye

Reputation: 43884

Your question is a little vague since you mix your English:

I simply restrict the query to only the data on the local mongos shard.

The shard will infact be a mongod process, not a mongos process, however your English can make sense if you have a mongos per shard in which case it makes sense that you want to direct to a mongos on that shard that can query its local mongod data.

If you are considering on circumventing the mongos then @Stennies comment answers your question however, if your English means something else then I do not believe the mongos has a command switch to allow you to direct queries without a shard key currently.

Upvotes: 0

Meny Issakov
Meny Issakov

Reputation: 1421

Please keep in mind, that doing so is unadvised as traffic to a shared cluster should go through a mongos service.

That being said, It's possible to query the shard itself if you're performing the query locally on the shard instance.

I've never tried to do that programatically, but It may worth a shot.
You can either login directly to the machine running the shard, and open a mongo shell there (if you've never created a local user/password on it, I believe you can connect without credentials, otherwise, the mongod process on that specific shard must have it's own user/pass (as those which were created via the mongos are not recognised in the mongod shards.

As each shard knows its own data files only, and for example you'll run a count() operation on one of your collection you'll see that the result is only a portion of the actual collection size.

Upvotes: 0

Related Questions