Contents
How do you define shard key?
The shard key is either a single indexed field or multiple fields covered by a compound index that determines the distribution of the collection’s documents among the cluster’s shards.
What is shard key in Cosmos DB?
A shard key in MongoDB maps to a partition key in Cosmos DB. When you create your collection, you’re asked to specify a partition key, if you create it via the portal. Or you can specify the shard key via MongoDB’s command line tool or via code (where it’s then called a shard key).
How do I create a composite key in MongoDB?
MongoDB and composite primary keys
- Use an object for the primary key that is made up of 2 values (as suggested here)
- Use a standard auto-generated mongo object id as the primary key, store my key in two separate fields, and then create a composite index on those two fields.
- Make the primary key a hash of the 2 uuids.
How do you shard a collection?
To shard a populated collection, the collection must have an index that starts with the shard key. When sharding an empty collection, MongoDB creates the supporting index if the collection does not already have an appropriate index for the specified shard key. See Shard Key Indexes.
Should I use cosmos DB?
Azure Cosmos DB is a global distributed, multi-model database that is used in a wide range of applications and use cases. It is a good choice for any serverless application that needs low order-of-millisecond response times, and needs to scale rapidly and globally.
How do I choose a cosmos DB partition key?
Selecting your partition key is a simple but important design choice in Azure Cosmos DB. Once you select your partition key, it is not possible to change it in-place. If you need to change your partition key, you should move your data to a new container with your new desired partition key.
Does MongoDB index order matter?
Indexes store references to fields in either ascending ( 1 ) or descending ( -1 ) sort order. For single-field indexes, the sort order of keys doesn’t matter because MongoDB can traverse the index in either direction.
Why is the _ ID field not part of the shard key?
Because the _id key is not part of the shard key, the collection could have a document with _id value 1 in shard A and another document with _id value 1 in shard B. If the _id field is not the shard key nor the prefix of the shard key, MongoDB expects applications to enforce the uniqueness of the _id values across the shards.
What to do if you drop an index on a shard key?
If the collection is not empty, you must create the index first before using sh.shardCollection (). If you drop the last valid index for the shard key, recover by recreating an index on just the shard key. MongoDB can enforce a uniqueness constraint on a ranged shard key index.
How are shard key values distributed in MongoDB?
MongoDB divides the span of shard key values (or hashed shard key values) into non-overlapping ranges of shard key values (or hashed shard key values). Each range is associated with a chunk, and MongoDB attempts to distribute chunks evenly among the shards in the cluster.