SELECT * FROM db WHERE ANY item IN images SATISFIES item.img = 'imgUrl1' END;
ANY or SOME, EVERY, and ANY AND EVERY or SOME AND EVERY
Range predicates (ANY or SOME, EVERY, and ANY AND EVERY or SOME AND EVERY) allow you to test a boolean condition over the elements or attributes of a collection or object(s). They each evaluate to a boolean value.
ANY or SOME is TRUE if the collection is non-empty and at least one element matches.
EVERY is TRUE if the collection is empty, or if the collection is non-empty and every element matches.
ANY AND EVERY or SOME AND EVERY is TRUE if the collection is non-empty and every element matches.
INSERT INTO default VALUES("k01",{"user":"roy","images":[{"img":"imgUrl1","date":"today"},{"img":"imgUrl2","date":"tommorrow"}]});
SELECT item.img
FROM default AS d
UNNEST d.images AS item
WHERE item.img = 'imgUrl1';
UPDATE default AS d
SET v.img = "ABC" FOR v IN d.images WHEN v.img = "imgUrl1" END
WHERE ANY item IN d.images SATISFIES item.img = "imgUrl1" END;
WHERE clause decides which documents qualify for update. If you don’t have WHERE clause every document is qualified for update, document actually modified or not decided by SET clause.
WHEN clause is inside the SET. WHEN decides which portion of the array object needs to update.
If bucket has 100 documents and 50 of the have have array images and 10 of those has img = “imgUrl1”. Without the WHERE clause all 100 will qualify for update and 10 of them modifies img all others may be updated same document.
If there is WHERE clause 10 of them will qualify for update.
Example: with WHERE clause and following index only images that has item.img = ‘imgUrl1’ will be scanned and fetched and updated. All others will not even Scanned by indexer and query will much faster. NOTE: variable used here item in UNNEST/ANY needs to be matched with Index.
CREATE INDEX ix1 ON default( DISTINCT ARRAY item.img FOR item IN images END);