4

On my CosmosDB, I've this json document:

--- Category example on Container Categories
{
    "id": "expert_talks",
    "name": "Expert Talks",
 }

--- Timeline example on Container Timeline
    "id": "30122021170000",
    "type": "PDF",
    "datePublished": "2021-12-29T16:00:00.944Z",
    "title": "The Title2",
    "subTitle": "The Subtitle2",
    "category": "expert_talks",
    "tags": [
        "Alpha",
        "Bravo",
        "Charlie",
        "Delta",
        "Echo",
        "Fox"
    ],
    "file": "https://africau.edu/images/default/sample.pdf"
}

So, a timeline entry points to a key that's exists on the category container.

Now, this is my mode... There is 2 models and a call to CosmosDb to bring me results and then I should feed a List of Timeline entries.

// My models
    public class TimelineItem
    {
        public string Id { get; set; }
        public string Type { get; set; }
        public string Title { get; set; }
        public string SubTitle { get; set; }
        public string File { get; set; }
        public Category Category { get; set; }
        public DateTime DatePublished { get; set; }
        public List<People>? People { get; set; }
        public List<string> Tags { get; set; }
    }
public class Category
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

// The code that's make the call
public async Task<List<TimelineItem>> GetTimeline()
        {
            string sqlQueryText = $"SELECT * FROM c order by c.id";
            QueryDefinition queryDefinition = new QueryDefinition(sqlQueryText);
            FeedIterator<TimelineItem> queryResultSetIterator = this.timelineContainer.GetItemQueryIterator<TimelineItem>(queryDefinition);
            List<TimelineItem> items = new List<TimelineItem>();
            while (queryResultSetIterator.HasMoreResults)
            {
                FeedResponse<TimelineItem> currentResultSet = await queryResultSetIterator.ReadNextAsync();
                foreach (TimelineItem item in currentResultSet)
                {
                    items.Add(item);
                }
            }
            return items;
        }

The problem is in this line: FeedResponse currentResultSet = await queryResultSetIterator.ReadNextAsync();

Because on the result from the json database, Category is a string, not an object. So, I ot this error: Error converting value "expert_talks" to type 'IsraPharmaExp.Models.Category'. Path '[0].category', line 1, position 155.

The error make sense to me, but I've no cluse about how to solve it. Is there some way to make a SQL Call to Cosmos and bring me the results in the specifcil model shape like doing some relational SQL Query for this ?

If not: Any chance to send the categories as parameters like public async Task<List> GetTimeline(Categorty allOfMyCategories) and in some how use the data inside the function ?

How to solve this simple issue ? thanks in advance !

0xced
  • 25,219
  • 10
  • 103
  • 255
Marco Jr
  • 6,496
  • 11
  • 47
  • 86

1 Answers1

0

The way this data is stored is incorrect for how you want to query and deserialize into your objects. If you want to embed an object inside of another object it needs to be stored in the format below so you can deserialize it into these two objects as shown above.

{    
"id": "30122021170000",
"type": "PDF",
"datePublished": "2021-12-29T16:00:00.944Z",
"title": "The Title2",
"subTitle": "The Subtitle2",
"category": {
    "id": "expert_talks",
    "name": "Expert Talks"
},
"tags": [
    "Alpha",
    "Bravo",
    "Charlie",
    "Delta",
    "Echo",
    "Fox"
],
"file": "https://africau.edu/images/default/sample.pdf"
}
Mark Brown
  • 8,113
  • 2
  • 17
  • 21
  • I don't agree...category can be used in may others documents with many others purposes. So, a reference to a key is a good option. – Marco Jr Dec 16 '21 at 23:26
  • I don't get what you aren't agreeing with. If you want to use that class hierarchy and read/write data in an out of Cosmos with the query you have, the data needs to be stored as I described it. – Mark Brown Dec 16 '21 at 23:33
  • Yeah, ofc....a database must obey the application....Right..why not ? LOL – Marco Jr Dec 21 '21 at 19:44