Reputation: 37
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
Reputation: 65323
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:
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.
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.
mongos
?No. All updates to a sharded collection must go through mongos
.
Upvotes: 1
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
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