Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cosmos DB SQL Query for nested objects

I have accounts collection in Cosmos DB. I tried different queries but failed what will be equivalent SQL Query to fetch only accounts which has selected subscription.

I tried this Query but failed

SELECT     *
FROM account a
JOIN s IN c.subscriptions
WHERE s.id = "e5969a3c-2729-cb3c-a01b-2e62e0473646"

Account Collection Records

[
  {
    "id": "8c549b95-480e-47f9-acd6-13339179399f",
    "odoo_id": "UpdatedDAta",
    "entity_name": "Lakes High School123",
    "entity_type": "family | teacher | school | district",
    "contacts": [
      {
        "name": "Mr. Garcia1",
        "email": "[email protected]"
      },
      {
        "name": "Mr. Garcia3",
        "email": "[email protected]"
      }
    ],
    "subscriptions": [
      {
        "id": null,
        "type": "group | profile",
        "group_name": "Year 4",
        "teachers": [
          "Ms Jones"
        ],
        "start_date": "25/7/2018",
        "end_date": "24/7/2019",
        "seats": 4,
        "group_key": "red-limping-pigeon"
      },
      {
        "id": "e5969a3c-2729-cb3c-a01b-2e62e0473646",
        "type": "group | profile",
        "group_name": "Year 4",
        "teachers": [
          "Ms Jones",
          "Waqar"
        ],
        "start_date": "25/7/2018",
        "end_date": "24/7/2021",
        "seats": 4,
        "group_key": "red-limping-pigeon"
      }
    ],
    "_rid": "bjcNANQrW3oGAAAAAAAAAA==",
    "_self": "dbs/bjcNAA==/colls/bjcNANQrW3o=/docs/bjcNANQrW3oGAAAAAAAAAA==/",
    "_etag": "\"01001c87-0000-0000-0000-5b7966850000\"",
    "_attachments": "attachments/",
    "_ts": 1534682757
  }
]
like image 980
SOF User Avatar asked Aug 19 '18 14:08

SOF User


1 Answers

Please use below sql to fetch your documents:

SELECT * FROM c
where ARRAY_CONTAINS(c.subscriptions,{"id": "e5969a3c-2729-cb3c-a01b-2e62e0473646"},true)

Array Contains could return a Boolean indicating whether the array contains the specified value.

Hope it helps you.

like image 178
Jay Gong Avatar answered Sep 22 '22 19:09

Jay Gong