0

Background:

I wish to locate the entire JSON document that has a condition where "state" = "new" and where length(Features.id) > 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:

A stackoverflow member has helped me to access the nested contents within the query, but is there a way to obtain the full document

I have used:

  SELECT VALUE t.id FROM t IN f.feedback.Features where t.state = 'new' and length(t.id)>4

This will give me the ids.

My desire is to have access to the full document with this condition?

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

            }

        ]
    }
}

Any help is appreciated

3
  • 1
    Try returning the f variable in the select query. Commented Aug 19, 2020 at 4:40
  • like this? SELECT VALUE f.id FROM t IN f.feedback.Features where t.state = 'new' and length(t.id)>4 Commented Aug 19, 2020 at 4:46
  • Just this should do if you want the entire data again. SELECT VALUE f FROM t IN f.feedback.Features where t.state = 'new' ... Commented Aug 19, 2020 at 4:50

1 Answer 1

1

Try this

SELECT * 
FROM f 
WHERE 
    f.feedback.Features[0].state = 'new' 
    AND length(f.feedback.Features[0].id)>4

Here is the SELECT spec for CosmosDB for more details

https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-select

Also, check out "working with JSON" in CosmosDB notes

https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-working-with-json

If the Features array has more than 1 value, you can use EXISTS clause to search within them. See specs of EXISTS here with examples:

https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-subquery#exists-expression

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

1 Comment

ok thank you. This did not work for me. Perhaps its not working because the 'Features' is an array?

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.