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"
}
Juanjo Rodriguez
  • 2,103
  • 8
  • 19
ceb
  • 29
  • 2

1 Answers1

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;
vsr
  • 7,149
  • 1
  • 11
  • 10
  • 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); – ceb May 19 '21 at 12:23