The following JSON represents two documents in a Cosmos DB container. How can I write a query that gets all the documents that have a date with type = "SubmittedDate" and its value between 2020-01-01 and 2024-05-01?
[{
"episodeID": "A500057110",
"serviceRequest": {
"listOfServiceRequest": {
"serviceRequestKey": "A500057110",
"dates": [
{
"type": "SubmittedDate",
"value": "03/26/2025 15:50:11"
},
{
"type": "DueDate",
"value": "05/15/2026 15:50:11"
}
],
"requestStatus": "Cancelled"
},
"clientSystem": "Two"
},
"id": "fcfd621e-9c7b-0b40-0a86-4962d7bbe54d"
},
{
"episodeID": "B340057110",
"serviceRequest": {
"listOfServiceRequest: {
"serviceRequestKey": "C214354353",
"dates": [
{
"type": "SubmittedDate",
"value": "01/16/2024 15:50:11"
},
{
"type": "DueDate",
"value": "07/01/2028 15:50:11"
}
],
"requestStatus": "Approved"
},
"clientSystem": "One"
},
"id": "fcfd621e-9c7b-0b40-0a86-4962d7bbe54d"
}]
I tried with below query, but the result is empty
SELECT * FROM c
WHERE c.serviceRequest.listOfServiceRequest.dates <= '{{type: SubmittedDate, value: 01/01/2020}}' and c.serviceRequest.listOfServiceRequest.dates >= '{{type: SubmittedDate, value: 01/01/2024}}'
The output I need is
{
"episodeID": "B340057110",
"serviceRequest": {
"listOfServiceRequest: {
"serviceRequestKey": "C214354353",
"dates": [
{
"type": "SubmittedDate",
"value": "01/16/2024 15:50:11"
},
{
"type": "DueDate",
"value": "07/01/2028 15:50:11"
}
],
"requestStatus": "Approved"
},
"clientSystem": "One"
},
"id": "fcfd621e-9c7b-0b40-0a86-4962d7bbe54d"
}
It is providing empty result because of many aspects, check the variables
used in the query are matched with the variables stored in the cosmos db container. In the query you tried is using listOfServiceRequests and the data you stored is using listOfServiceRequest. Also, try to follow the same syntax while storing the data into the container, as per this article, cosmos db is following this syntax format to store the date yyyy-MM-ddTHH:mm:ss.fffffffZ
. In the same way i stored in my azure cosmos db container and tried with the below query using JOIN
clause and successfully retrieved the expected data as shown in the below output.
SELECT c
FROM c
JOIN s IN c.serviceRequest.listOfServiceRequest.dates
WHERE s["value"] >= "2024-01-01T00:00:00.0000000Z"
AND s["value"] <= "2025-01-01T00:00:00.0000000Z"
Output:
[
{
"c": {
"episodeID": "B340057110",
"serviceRequest": {
"listOfServiceRequest": {
"serviceRequestKey": "C214354353",
"dates": [
{
"type": "SubmittedDate",
"value": "2024-16-01T15:50:11.0000000Z"
},
{
"type": "DueDate",
"value": "2028-01-07T15:50:11.0000000Z"
}
],
"requestStatus": "Approved"
},
"clientSystem": "One"
},
"id": "fcfd621e-9c7b-0b40-0a86-4962d7bbe54d"
}
}
]