I have a query that dynamically determines a collection name and an expression to evaluate on that collection that return a boolean. Say:
$my-collectionpoints to the collection, e.g. containing the string/db/my/collection.- The boolean expression is
exists(/foo/bar).
I can run exists(/foo/bar) on the collection itself, which will return either true or false, depending on whether a document in the collection contains a /foo/bar. But how can I do the same when the collection name isn’t known in advance?
Naively, I tried collection($my-collection)/exists(/foo/bar). But since collection() returns the document nodes in the collection, this would return as many booleans as there are documents in the collection, instead of just one boolean. This isn’t what I want, plus it can be extremely slow as my collection can contain several tens of thousands of documents.
So, how should I write this instead?
You can rewrite your expression to this:
Or perhaps this, which – depending on the eXists query optimizer – might perform better:
HTH!