Subject: [mongodb-user] Re: How index working in shard
concept?



Hi,

For Ex: Index column is “Name” , name “john” is in 4 shard out of 10
index will stored in all shard and search query will hit all shard or only it will hit the 4 shard

For a sharded collection, MongoDB requires you to define a shard key for the collection. To paraphrase from the linked page: The shard key determines the distribution of the collection’s documents among the cluster’s shards.

Using your index {name: 1} example, there are three possible scenario with this:

  • {name: 1} is a secondary index on the collection, i.e. not part of the collection’s shard key. In this case, searching for db.collection.find({name: "John"}) will be a scatter-gather query. That is, all 10 shards will search for John, and the ones that contain John will report back.

  • {name: 1} is part of a compound shard key, e.g. {name: 1, city: 1}. In this case, searching for db.collection.find({name: "John", city: "Sydney"}) will be targeted to the shard that contains the chunk with that key. Due to the definition of a shard key, there can only be a single chunk containing {name: "John", city: "Sydney"}, and this chunk can only reside in a single shard. Therefore, the query will be targeted to a single shard.

  • {name: 1} is part of a compound shard key, e.g. {name: 1, city: 1}, but you’re searching for db.collection.find({name: "John"}). Since name is a prefix of the shard key, the query can go to all shards where John exists, e.g. it is possible that {name: "John", city: "SF"} exists in one shard, while {name: "John", city: "Sydney"} exists in another shard. If John exists in 4 shards, then the query will go to only those 4 shards.

  • More information can be found in:

    Best regards,
    Kevin

    --
    You received this message because you are subscribed to the Google Groups "mongodb-user"
    group.
     
    For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
    ---
    You received this message because you are subscribed to the Google Groups "mongodb-user" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    To post to this group, send email to [email protected].
    Visit this group at https://groups.google.com/group/mongodb-user.
    To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/62467df3-2ebe-4362-9d73-6bf366b9b9c1%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.



    Programming list archiving by: Enterprise Git Hosting