How do I read a single item from CosmosDb using Microsoft.Azure.Cosmos v3.9.1

Viewed 5241

I'm using CosmosDB for the first time, and I am attempting to read a single document by id, according to this documentation:

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.container.readitemasync?view=azure-dotnet

According to this documentation, my container object should have a ReadItemAsync method that I can use to find and read a single item. However, when I import the "Microsoft.Azure.Cosmos" nuget package to my project and attempt to use this code snippet, there is no function on the container for ReadItemAsync. Here is a snippet of my code:

    using Microsoft.Azure.Cosmos;
        ...
    var databaseId = "{databaseId}";
    var containerId = "{containerId}";
    var client = new CosmosClient(model.IndexDatabaseUri, model.IndexDatabasePrimaryKey);
    var database = client.GetDatabase(databaseId);
    var container = database.GetContainer(containerId);
    var call = container... // NO ReadItemAsync HERE

I do see these functions on the container:

    GetItemLinqQueryable
    GetItemQueryIterator
    GetItemQueryStreamIterator

From the looks of it, I could probably create a query iterator with a custom SELECT statement, and retrieve the first item in the iterator. But that seems overkill for what I am wanting to do. I will always only want a single item in this instance. Why is there no ReadItemAsync method here, when it shows in the documentation that there should be one? And, if the "Iterator" is the new, preferred method, are there any performance considerations in using QueryIterator vs retrieving a single document? This function will be run millions of times over the span of one day.

p.s. - I am using the "Microsoft.Azure.Cosmos" nuget package v3.9.1

1 Answers

If you are reading 1 item and you know the id and Partition Key, then that's the fastest and cheapest (in terms of RU/s) way to read the information. Doing a Query with a WHERE clause with the id would be more expensive.

I just created a NET Core Console Application with 3.9.1 and the method is there, and that documentation you pointed at gets automatically generated based off the DLLs in the Nuget packages.

enter image description here

Related