ceb
ceb

Reputation: 39

Couchbase N1QL nested json query with variable key

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"
}

Upvotes: 1

Views: 572

Answers (1)

vsr
vsr

Reputation: 7414

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;

Upvotes: 1

Related Questions