我应该如何使用 v3 API 从 Cosmos 获取一项?

问题描述 投票:0回答:2

我想从 Cosmos DB 检索一项,肯定有比我在这里所做的更好的方法。

我尝试过其他命令,但这似乎确实有效。

    public async Task<ToDoItem> GetAsync(string id)
    {
        FeedIterator<ToDoItem> results = container.GetItemQueryIterator<ToDoItem>("select top 1 * from Items i where i.id = '" + id + "'");

        FeedResponse<ToDoItem> item = await results.ReadNextAsync();

        return item.Resource.FirstOrDefault();
    }

我希望能够通过在 服务器并且不会强迫我查看一组项目。

azure-cosmosdb azure-cosmosdb-sqlapi
2个回答
1
投票

这里是官方文档的例子

query

 using (ResponseMessage responseMessage = await container.ReadItemStreamAsync(
                partitionKey: new PartitionKey("Account1"),
                id: "SalesOrder1"))

0
投票

SDK 仍在开发中 - 这可能会有所帮助:

using (ResponseMessage response = await _container.ReadItemStreamAsync(id: pageId, partitionKey: partitionKey))
{
    if (!response.IsSuccessStatusCode)
    {
        //Handle and log exception
    }

    await using (Stream stream = response.Content)
    {
        using (var streamReader = new StreamReader(stream))
        {
            string content = streamReader.ReadToEnd();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.