Querying tags with Cosmos DB with EF Core

Ricardo Peres 1 Reputation point
2020-11-05T11:50:54.967+00:00

Hi all,
I have a domain model that is persisted with EF Core and the Cosmos DB provider. The problem is, I would like to add to it tags, which I would like to query, e.g., return all entries that have this tag. Unfortunately, Cosmos DB does not support string arrays (among others), so I can only imagine using a single string property, which is far from ideal, because if I need multiple tags I would need to combine them on the same property, probably separated by spaces, and this would make querying of a single one very difficult.
What are the best options for achieving this?
I also asked this on Stackoverflow: https://stackoverflow.com/questions/64676788/querying-tags-with-cosmos-db-with-ef-core.
Thanks!

RP

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,604 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,321 Reputation points
    2020-11-06T09:35:48.827+00:00

    @Ricardo Peres Welcome to Microsoft Q&A and thank you for your interest in Azure CosmosDB.

    Here is an example of a document containing a string array of tags

    {  
      "product" : "SampleProduct",  
      "tags" : ["tag1", "tag2"]  
    }  
    

    You should be able to query for documents using the below queries

    SELECT * FROM c JOIN tags IN c.tags WHERE tags IN ("tag1")  
    SELECT * FROM c JOIN tags IN c.tags WHERE ARRAY_CONTAINS([tags], "tag1")  
    

    I don't completely understand what you mean by "Cosmos DB does not support string arrays".
    Please let me know if my interpretation of your question is incorrect and we will help you accordingly.

    ----------

    If an answer is helpful, please "Accept answer" or "Up-Vote" for the same which might be beneficial to other community members reading this thread.

    0 comments No comments

  2. Stephan van Rooij 61 Reputation points MVP
    2020-11-13T10:05:02.07+00:00

    @KalyanChanumolu-MSFT what @Ricardo Peres is trying to say is:

    I'm using EF Core together with Cosmos DB. There is a class (posts for instance) with a property Tags with the type IEnumerable<string>, how do I get EF Core to store those Tags in the Post document (as described by yourself).

    I'm also struggling with this.

    The following page describes Embedding Entities but the samples don't include a way to embed an IEnumerable of string.

    https://learn.microsoft.com/en-us/ef/core/providers/cosmos/?tabs=dotnet-core-cli#embedded-entities

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.