I have two queries that work in Cosmos. But now I want to combine them into one query. How do I do that?
As you want to combine the two queries, in the first query it is retrieving only the data which is "id = LanguageList-V3"
and in the second query it is retrieving the data having languageId = "af"
. Means after combining two queries using JOIN, it will print only the data of id having LanguageList-V3
and with languageId = "af"
.
Also, you mentioned There are other version of the same list in the table
, below is the data stored in my cosmos db container with some other versions along with id "LanguageList-V3"
such as "LanguageList-V1"
and "LanguageList-V2"
:
[
{
"id": "LanguageList-V1",
"type": "LanguageList",
"version": 1,
"createdAt": "2023-01-01T00:00:00Z",
"Languages": [
{
"languageId": "af",
"englishName": "Afrikaans",
"nativeName": "Afrikaans",
"active": true
},
{
"languageId": "es",
"englishName": "Spanish",
"nativeName": "Español",
"active": true
}
]
},
{
"id": "LanguageList-V2",
"type": "LanguageList",
"version": 2,
"createdAt": "2024-01-01T00:00:00Z",
"Languages": [
{
"languageId": "es",
"englishName": "Spanish",
"nativeName": "Español",
"active": false
},
{
"languageId": "zh",
"englishName": "Chinese",
"nativeName": "中文",
"active": true
}
]
},
{
"id": "LanguageList-V3",
"type": "LanguageList",
"version": 3,
"createdAt": "2025-04-21T12:00:00Z",
"Languages": [
{
"languageId": "af",
"englishName": "Afrikaans",
"nativeName": "Afrikaans",
"active": false
},
{
"languageId": "zh",
"englishName": "Chinese",
"nativeName": "中文",
"active": true
}
]
}
]
Below is the query which i tried to combine both the queries using JOIN:
SELECT VALUE l
FROM c
JOIN l IN c.Languages
WHERE c.id = "LanguageList-V3" AND l.languageId = "af"
Output:
[
{
"languageId": "af",
"englishName": "Afrikaans",
"nativeName": "Afrikaans",
"active": false
}
]
Note: Cosmos DB does not support joins across different containers or items. It supports self-join
,means joins occur within a single item. For more information, please refer to this link.