Is the partition key required when retrieving by t

2020-07-14 09:48发布

Is it possible to retrieve a document by its ID without specifying the partition key?

My understanding from reading the documentation is that the query will fan out across all partitions when the partition key is not specified:

The following query does not have a filter on the partition key (DeviceId) and is fanned out to all partitions where it is executed against the partition's index. Note that you have to specify the EnableCrossPartitionQuery (x-ms-documentdb-query-enablecrosspartition in the REST API) to have the SDK to execute a query across partitions.

This makes sense with non-key properties, but given the ID is treated specially, I'm hoping I won't need to enable cross partition queries for it.

If I do need to enable cross partition queries, would this be an expensive operation?

1条回答
老娘就宠你
2楼-- · 2020-07-14 10:18

Query by just ID will be a cross partition operation. You should include the partition key in these queries in FeedOptions.PartitionKey, or as part of the filter. In DocumentDB, ID is not unique across all documents within a collection. Instead, the combination of "partition key" and "id" is the primary key and uniquely identifies documents within a collection.

Some applications encode partition key as part of the ID, e.g. partition key would be customer ID, and ID = "customer_id.order_id", so you can extract the partition key from the ID value.

查看更多
登录 后发表回答