I'm trying to query a database where the stored entities have different properties and I want to retrieve some fields from this information with diferently shaped queries (thats the point of using an schemaless database) but I'm having no luck with CosmosDB.
As I don't have a class (nor I want) for each kind of query result, I want to get them as a list of dynamics but I don't know how to retrieve them. I've tried:
var queryResultSetIterator = container.GetItemQueryIterator<dynamic>(queryDefinition, null, new QueryRequestOptions() { PartitionKey = new PartitionKey(idParticion), MaxItemCount = -1 });
List<dynamic> results = new List<dynamic>();
await foreach (dynamic item in queryResultSetIterator)
{
results.Add(item);
}
But the items I get are of type System.Text.Json.JsonDocument so it doesn't work as expected;
If I try with ExpandoObjects I can't deserialize the results with new new System.Text.Json serializer, because as of today it doesn't support deserializing to ExpandoObjects:
results.Add(JsonSerializer.Deserialize<ExpandoObject>(item));
The only way it works for now is using the old Newtonsoft Json serializer but it seems to me an overkill to have both libraries present on the application, with all of that implies.
Any other idea of how to obtain a dynamic projection from CosmosDB?
Thanks in advance!