[英]Cosmos DB (DocumentDB API): Efficient way to query most recent document by partition ID?
[英]What is the most efficient way to query all documents from a specific partition in Partitioned Collection in Azure Document DB?
我在Document db中創建了一個Partitioned Collection,我希望能夠從該Collection中查詢特定分區中的所有文檔,而無需進行跨分區查詢。 例如,假設我正在將Car對象寫入此Collection,並且Partition Key字段是Car.Manufacturer。 制造商是一個字符串。
我想從我的分區集合中查詢屬於特定制造商的所有Car文檔。
最有效的方法是什么? 目前,我正在執行以下操作,並且我知道這會導致交叉分區查詢-即使我在查詢中明確設置了分區鍵(設置為“ Mercedes”)
IDocumentQuery<Car> query = documentClient.CreateDocumentQuery<Car>(
UriFactory.CreateDocumentCollectionUri("dbName", "CollectionName"))
.Where(c => c.Manufacturer == "Mercedes")
.AsDocumentQuery();
List<Car> mercedesCars = new List<Car>();
while (query.HasMoreResults)
{
foreach (Car car in await query.ExecuteNextAsync<Car>())
{
data.Add(car);
}
}
我正在尋找一種解決方案,使該查詢不會引起跨分區查詢?
您可以通過向FeedOptions
提供查詢並設置PartitionKey
屬性來指定分區。
您的查詢將如下所示:
var feedOptions = new FeedOptions { PartitionKey = new PartitionKey("Mercedes") };
IDocumentQuery<Car> query = documentClient.CreateDocumentQuery<Car>(
UriFactory.CreateDocumentCollectionUri("dbName", "CollectionName"), feedOptions)
.Where(c => c.Manufacturer == "Mercedes")
.AsDocumentQuery();
List<Car> mercedesCars = new List<Car>();
while (query.HasMoreResults)
{
foreach (Car car in await query.ExecuteNextAsync<Car>())
{
data.Add(car);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.