0

Need to select array elements as row element.

Cosmos Documents JSON

1.

{
  "CountyId": 1
  "CountyCode": "Abbeville",
  "Cities": [
    { "CityId": 1, "CityName": "Arborville" }
  ]
}

2.

{
  "CountyId": 2
  "CountyCode": "Adair",
  "Cities": [
    { "CityId": 2, "CityName": "Ballard" },
    { "CityId": 3, "CityName": "Brashear" },
  ]
}

And the result that I need would like this.

enter image description here

2
  • 1
    Please edit your question to show what you've tried, where you're stuck, expected vs actual output, etc. As for a place to start: have you looked at doing a self-join (described in the Cosmos DB documentation)? Commented Apr 25, 2020 at 13:00
  • Thanks for your suggestion, I am new to the Cosmos DB so was not aware of it. Commented Apr 26, 2020 at 5:50

1 Answer 1

1

Please try the following query:

SELECT c.CountyId, c.CountyCode, d.CityId, d.CityName FROM c
Join d in c.Cities

This produces the following output:

[
    {
        "CountyId": 1,
        "CountyCode": "Abbeville",
        "CityId": 1,
        "CityName": "Arborville"
    },
    {
        "CountyId": 2,
        "CountyCode": "Adair",
        "CityId": 2,
        "CityName": "Ballard"
    },
    {
        "CountyId": 2,
        "CountyCode": "Adair",
        "CityId": 3,
        "CityName": "Brashear"
    }
]

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

that is what I was looking for, Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.