Cosmos查询Item子集合时的Continuation Token/MaxItems

huangapple go评论143阅读模式
英文:

Cosmos Continuation Token/MaxItems when querying Item sub collection

问题

Cosmos新手问题在这里。我有一个名为Orders的容器,集合中的每个订单项都有以下结构:

{
   "id":"9b461e2f-3728-46f0-9383-103be9e225f9",
   "agentId":"4v587r3h-3728-46f0-9383-103be9e225f9",
   "OrderItems": [{
     "OrderItemId":"8a378g5w-3728-46f0-9383-103be9e225f9",
     "ProductId": 100, 
     "Qty": 3
   }]
}

类似于

//通过API调用传入了agencyId、continuationToken、paginationNumber。Cosmos生成了令牌

var result = await container.GetItemLinqQueryable<Order>(continuationToken: continuationToken,
 requestOptions: new QueryRequetOptions() 
{ 
 PartitionKey = new PartitionKey("someKey"), 
 MaxItems = paginationNumber
}).Where(o => o.AgencyId == agencyId).ToFeedIterator().ToListyAsycnWithContinue();

我看到了一些示例,展示了如何查询此集合,返回一个项目类型的列表,即Orders,以及一个continuationToken,可以通过API公开,以允许API的客户端通过将continuationToken添加到任何未来请求中来分页结果,然后Cosmos可以继续查询并观察最大项数。

我想查询此集合,返回由特定AgentId下的所有OrderItems。在这里,continuationToken/maxItems值如何发挥作用?我的意图是它们将指的是OrderItems,而不是Order级别。因此,如果maxItems为20,我是说将此限制为20个OrderItems,而不是20个Orders。

英文:

Cosmos newbie question here. I have a Container called Orders and each Order Item in the Collection has a structure like

{
   &quot;id&quot;:&quot;9b461e2f-3728-46f0-9383-103be9e225f9&quot;
   &quot;agentId&quot;:&quot;4v587r3h-3728-46f0-9383-103be9e225f9&quot;
   &quot;OrderItems&quot;: [{
     &quot;OrderItemId&quot;:&quot;8a378g5w-3728-46f0-9383-103be9e225f9&quot;,
     &quot;ProductId&quot;: 100, 
     &quot;Qty&quot;: 3
   }]
}

Something like

//agencyId, continuationToken, paginationNumber came in via API call. Cosmos generated the tokens

var result = await container.GetItemLinqQueryable&lt;Order&gt;(continuationToken: continuationToken,
 requestOptions: new QueryRequetOptions() 
{ 
 PartitionKey = new PartitionKey(&quot;someKey&quot;), 
 MaxItems = paginationNumber
}).Where(o =&gt; o.AgencyId == agencyId).ToFeedIterator().ToListyAsycnWithContinue();

I've seen examples that show how to query this Collection where it returns a list of the Item type, ie Orders and a continuationToken which could be exposed via an API to allow a client of that API a means to paginate results by adding the continuationToken to any future requests and Cosmos could then resume the query observing a MaxCount items.

I'd like to query this Collection to return all the OrderItems that were placed by a particular AgentId. Where does the continuationToken/maxItems values come into play here as my intent would be that they'd be referring to the OrderItems not at the Order level. So if maxItems was 20 for example, I'm saying restrict this to 20 OrderItems not 20 Orders.

答案1

得分: 0

continuationToken对于这个目的并不有用。它仅用于从查询中检索附加文档,而不是文档内的子元素。continuationToken用于跟踪查询的状态,以便您可以从查询中的最后一个文档继续,而无需逻辑地迭代查询之前的所有文档。

如果您想在文档内分页显示信息,您需要自己编写查询(例如,让客户端传递偏移量和限制,并使用ARRAY_SLICE来检索相关的OrderItems)。

由于理想情况下您的文档很小,您还可以选择检索所有的OrderItems,并在客户端进行过滤,以便一次只显示一页,但所有的OrderItems都在单个请求中检索并保存在内存中。使用点读取和小型文档,您可以将 RU 使用量最小化。

最后,您还可以选择更改结构,并将每个OrderItem保存为单独的文档。当分页是您唯一的问题时,这可能不是最佳选择。

英文:

The continuationToken isn't useful for that. It's purely for retrieving additional documents from a query and not subelements within a documents. The continuationToken is used to keep track of the state of query so you can continue from the last document in the query without logicaly iterating over all documents queried before it.

If you want to paginate over information within a document you'll need to write a query for that yourself (e.g. having the client passing the offset and limit and using ARRAY_SLICE to retrieve the relevant OrderItems).

Since you ideally have kept your documents small you could also opt to retrieve all OrderItems and filter client side so only one page is viewable at a time, but all are retrieved in a single request and kept in memory. Using point reads and small documents you can keep your RU usage to a minimum this way.

Lastly you can also opt to change your structure and save each OrderItem as individual document. That'll of course have a lot of other effects on querying data. When pagination is your only issue then this likely isn't the best option.

huangapple
  • 本文由 发表于 2023年8月8日 21:33:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定