1

I have documents. How can I write a query with nested json field? Query: Count value greater than 3 output: doc-1 and doc-3

Doc-1

"1": {
    "count":4,
    "name": "pen"
}

Doc-2

"2": {
    "count":1,
    "name": "eraser"
}

Doc-3

"3": {
    "count":43,
    "name": "book"
}

1 Answer 1

1

Convert the dynamic object (OBJECT_VALUES(), OBJECT_NAMES(), OBJECT_PAIRS()) into ARRAY and use ANY clause https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/objectfun.html

SELECT b.*
FROM mybucket AS b
WHERE ANY v IN OBJECT_VALUES(b) SATISFIES v.`count` > 3 END;
Sign up to request clarification or add additional context in comments.

1 Comment

An index must be created for the query to run.: CREATE index idx_count ON mybucket (DISTINCT ARRAY v. count FOR v IN OBJECT_VALUES(mybucket) END);

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.