How to find docs that don't exist in another group.


I have a need to find documents in one group that don’t exist in another group of documents.

for $a in input()/doc1, $b in input()/doc2
where not ($a/id = $b/id)
return $a/id

I tried the above query but it got timeout, even though id is indexed.
Can you tell me what is the best format for doing this.


I suspect there are faster ways to do this, but this should work:

for $a in input()/doc1
where not (for $b in input()/doc2 where $b/id = $a/id)
return $a

The query you used confirms that for every $a/id there exists a $b/id that does not match $a/id, so you would likely end up returning all $a’s.