I have a database structure that is similar to the following structure.

  • User collection
    • Sub collection0, with userId
    • Sub collection1, with userId
    • Sub collection2, with userId
      • and so on


User: {_id, firstName, lastName, ...}
Blog: {_id, userId, ...}
Post: {_id, userId, ...}

The term sub collection is not appropriate I guess. In the above example, in one zone, I like to have documents related to only one user. For example, If there is an user X, then in a zone named X, there must be only one user document in users collection, which is X's document, the blogs & posts collections in zone X must only have X's blogs & posts. This use case is outlined here.

Based on the above documentation, I've setup zones. For shard tag range, min & max keys must be provided. In my use case(user id, blog/post ids), it is not possible to define range. Also for user collection, there is no range at all, only one user must in one zone.

I am able to think of hacks like, defining non overlap-able ranges in the application, which I tried & works too. But, is there a better way to do this?

Thank you!


Your collections need "key" (like 'zone') what is set when documents need to be "zoned". There will be only so many different key values than you have shards. When user wants to be zoned, you update all users documents (at all sharded collections) with that "key". Those collections are sharded that zone key. You choose one of those shards to be "default" shard so, all those documents what don't have zone key set, goes there. This default shard have minkey and you choose some other shard have that maxkey.

Not the best solution, but...

| improve this answer | |
  • Something similar is what I am thinking about already. But, l know that is not best solution. Anyway, thanks for your time. – explorer May 22 '18 at 5:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.