Couchbase Lite 2 - query to get documents filtered by input array of strings


#1

I want to write a query for documents with certain “identifiers”.
The document has a property called “identifier”.
The query should take an array of identifiers and return documents that are matching them.
The Input array is not a property of the document. It is simply an array to create the query.

Input Array [“identifierA”, “identifierB”, “identifierC”]
Document property [“identifier” : String]

I managed it to query all documents without a filter:
QueryBuilder
.select(SelectResult.all())
.from(DataSource.database(database))
.where(Expression.property(DocumentCB.queryKey)
.equalTo(Expression.string(TicketInfoCardCB.document.type)))
.orderBy(Ordering.property(TicketInfoCardCB.CodingKeys.type.rawValue)

What I need is probably something like this:
.and(ArrayFunction.contains(ArrayExpression.any(Expression.string(“identifier”)), value: identifiers)
–> “identifiers” is an array of Strings

I’ve spent a whole day reading the documentation, your blogs and sample code but could not find what I need.

Kind regards,
Steven


#2

The query should be of the form “WHERE identifier IN (…list of identifiers…)”. I don’t recall the exact syntax in the public API for an “IN” expression, sorry. (I work on the query engine but not on the API layer.)


#3

i am having the same issue, but i don’t think its possible, you can test if a value exists in a document array but not if a property value is contained by a given array.
however since the api is flexible the in expression can be converted in a series of or clauses:

 val dbchanged= DatabaseChangeListener()
{


        var whereExpr:Expression?=null;
         for(docid in it.documentIDs)
         {
             if(whereExpr==null)
                 whereExpr= Expression.property("_id").equalTo(Expression.string(docid))
             else
                 whereExpr=whereExpr.or(Expression.property("_id").equalTo(Expression.string(docid)))
         }
 var qresults=QueryBuilder.select(SelectResult.all())
                .from(DataSource.database(_app?.dbase))
                .where(whereExpr)
                .execute().allResults()		 
 }

This should work just as an in , but could someone kind at couchbase tell us if there is a limit on how may or conditions can be put in an expression ?
Thanks,
Iulian.


#4

I don’t believe there is any limit imposed. But there may be other implications. How many conditions are you planning on chaining?


#5

about 40 expressions