DEV Community

Discussion on: Things I wish I knew before I got started with CosmosDB

Collapse
 
tanishahudson1 profile image
tanishahudson1

Hello, I am trying to access values within CosmosDB and have hit a roadblock. Any insight is appreciated:

I wish to locate all the nested fields within my JSON document that contain the "state" = "new" and where the "id" length > 4

      {
          "id": "123"
          "feedback" : {
             "Features" : [
                              {
                                "state":"new"
                                "id": "12345"

                              }


                          ]
     }

This is what I have tried to do:

Since this is a nested document. My query looks like this:

 SELECT
 c.feedback
 ARRAY(SELECT Features.state FROM c IN c.Features) AS Features FROM 
 c
 WHERE length(Features.id) >15

However, the syntax is not correct and I am currently researching and looking for examples for this case

Any help is appreciated

Collapse
 
petereysermans profile image
Peter Eysermans • Edited

Hello, this is the query you are looking for:

 SELECT c.feedback from c
 join a in c.feedback.Features
 where a.state = 'new'
and length(a.id) > 4

As you can see there is a join on the Features array and then the fields of that array can be queried.

There are also additional options on the ARRAY_CONTAINS which are interesting to look at if you are working with nested objects and nested arrays.