0

Right now I have the following code to query my CosmosDB:

var streamIterator = containerLogs.GetItemQueryStreamIterator("SELECT * FROM mycontainer");

while (streamIterator.HasMoreResults)
{
    var results = await streamIterator.ReadNextAsync();
    var stream = results.Content;
    using var reader = new StreamReader(stream);
    string data = await reader.ReadToEndAsync();

    // typeof what..?
    //var dbDocuments = JsonConvert.DeserializeObject<???>(data);
}

The variable data will contain the following JSON:

{
    "_rid": "wDhMAJ9xYHE=",
    "Documents": [{
        "id": "c05c4eee-32d4-458a-8ae8-d22dd0f93839",
        "CustomProperty1": "Value 1",
        "CustomProperty2": "Value 2",
        "_rid": "wDhMAJ9xYHECAAAAAAAAAA==",
        "_self": "dbs\/wDhMAA==\/colls\/wDhMAJ9xYHE=\/docs\/wDhMAJ9xYHECAAAAAAAAAA==\/",
        "_etag": "\"00000000-0000-0000-7167-28c107aa01d6\"",
        "_attachments": "attachments\/",
        "_ts": 1597319093
    }],
    "_count": 1
}

The Documents part is my actual "domain" model. How can I easily map this back to a list of my domain model class? I used the same class to write to CosmosDB.

The model looks like this

public class MyModel
{
    [JsonProperty(PropertyName = "id")]
    public Guid Id {get;set;}
    public string CustomProperty1 {get;set;}
    public string CustomProperty1 {get;set;}
}

So how can I query my CosmosDB so that it returns a list of this class?

2
  • Which SDK are you using? Where does streamIterator come from? Commented Aug 13, 2020 at 16:55
  • I have added the line where streamIterator comes from. I'm using the package Microsoft.Azure.Cosmos version 3.0.10 Commented Aug 13, 2020 at 20:41

1 Answer 1

1

Here are some samples that I found helpful. The simpler case is to used the typed GetItemQueryIterator<MyModel>, but there is also a stream deserialization example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.