2

I would like to find a better way to search for if documents in a collection have a property with more than 0 elements in the array, i.e. anything that isn't empty.

such as: select * from c where c.property = 'x' and array_length(c.child) > 0 and array_length(c.child.grandchild) > 0

The first arraylength works. Adding the second with just this dot notation doesn't work as I read somewhere else. How can I ensure that I can accomplish this. The grandchild will be anywhere from 0 to many number where it has a greater array length than 0.

Please let me know if more clarification is needed.

0

1 Answer 1

5

Please use below sql :

SELECT distinct c.id,c.name,c.child FROM c
join child in c.child 
 where array_length(c.child) > 0 
and array_length(child.grandchild) > 0

My sample documents:

[
    {
        "id": "1",
        "name": "Jay",
        "child": [
            {
                "name": "A",
                "grandchild": [
                    {
                        "name": "A1"
                    },
                    {
                        "name": "A2"
                    }
                ]
            },
            {
                "name": "B",
                "grandchild": [
                    {
                        "name": "B1"
                    },
                    {
                        "name": "B2"
                    }
                ]
            }
        ]
    },
    {
        "id": "2",
        "name": "Tom",
        "child": [
            {
                "name": "A",
                "grandchild": []
            },
            {
                "name": "B",
                "grandchild": []
            }
        ]
    }
]

Hope it helps you.

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

1 Comment

what about mongodb syntax?

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.