0

I have the following cosmosdb document:

{
  id: "id",
  outer: [
    {
      "inner": [ "a", "b", "c" ]
    },
    {
      "inner": [ "d", "e", "f" ]
    }
  ]
}

And I need to create a SQL request which would return all of the combined values of the "inner" arrays, like this:

{
   "allInners": [ "a", "b", "c", "d", "e", "f" ]
}

I was able to unwind the first array level using the "IN" operator, but I am not sure how unwind it one more level and to handle double or even triple nested arrays. The following is my subquery to aggregate those items

SELECT
  ... other stuff.
  ARRAY(SELECT VALUE innerObj.inner FROM innerObj IN c.outer) AS allInners,
  ...
FROM c

2 Answers 2

2

I found the following solution to my problem (using the nested "IN" and a subquery):

ARRAY(
   SELECT VALUE inner
   FROM inner IN (
      SELECT VALUE outers.inner
      FROM outers IN c.outer
  )
)
Sign up to request clarification or add additional context in comments.

Comments

1

Please try something like this sql:

select ARRAY(SELECT VALUE e FROM c join d in c["outer"] join e in d["inner"]) AS allInners from c 

Here is result of my test: enter image description here

Hope this can help you.:)

1 Comment

get each item in inner array,and put them into a new array.You can run the sql:SELECT c,d,e FROM c join d in c["outer"] join e in d["inner"] to look at documents look like.More details, you can refer to arrays and objects and joins.

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.