kdeez
kdeez

Reputation: 711

CosmosDB WHERE clause for searching string array

I have cosmos db items that are structured like this:

      {
            "id": "ec79ff89-13a2-4da9-9a8d-e0ccc8e55f8e",
            "AttachmentIds": [
                "aa0805b7-e5f9-42ee-8cb9-07beadaaed31",
                "15072c53-3c4b-47c2-ac91-8d9d3138bf96"
            ],
            "_ts": 1589411571
     }

I just want to write a simple query that returns the items that have an "AttachmentId" containing "aa0805b7-e5f9-42ee-8cb9-07beadaaed31". I cannot seem to get it to work. This is what I have so far, but I'm getting a syntax error:

SELECT * FROM c where ARRAY_CONTAINS(c.AttachmentIds, {"aa0805b7-e5f9-42ee-8cb9-07beadaaed31"}, true)

Any help is greatly appreciated.

Upvotes: 0

Views: 1298

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136146

Please change your query to

SELECT * FROM c where ARRAY_CONTAINS(c.AttachmentIds, "aa0805b7-e5f9-42ee-8cb9-07beadaaed31", true)

and that should fix the problem you're having. I simply removed the curly braces from your query as you're trying to find a simple value.

You can find more information about ARRAY_CONTAINS here: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-array-contains.

Upvotes: 1

Related Questions