How to search JSON in depth in Azure Cosmos DB?

£可爱£侵袭症+ 提交于 2020-03-18 08:39:14

问题


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.


回答1:


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




回答2:


It will be just

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


来源:https://stackoverflow.com/questions/60373935/how-to-search-json-in-depth-in-azure-cosmos-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!