Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search JSON in depth in Azure Cosmos DB?

I have JSON files like below stored in Cosmos DB. I want to search text in activities/message.

{
  "id": "575858a7-f814-41fd-ae5a-6f38ba2da957",
  "name": "Test Name",
  "activities": [
    {
      "message": "activity 1.1 message",
      "messageType": "type1"
    },
    {
      "message": "activity 1.2 message",
      "messageType": "type2"
    }
  ]
}

I find out that I can use search like below.

SELECT * FROM c
WHERE CONTAINS(c.activities[0].message, "activity")

But, this can only search the first record in the array of activities. How to search all records in the array? Thanks.

like image 592
Huodong Avatar asked Oct 30 '25 04:10

Huodong


1 Answers

You need to apply a join on your query, so then you array will be treated as a normalized entity and then you can apply the filter.

select c.id, a.message
from c join a in c.activities
where CONTAINS(a.message,"activity")

More info

like image 83
Hugo Barona Avatar answered Nov 02 '25 23:11

Hugo Barona