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.
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.
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.)
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.
Your In list needs to be an array of Expression.String
This worked for me…
//String array
string[] identifiers = new string[] {"identifierA", "identifierB", "identifierC"};
//Initialise your Expression array
IExpression[] expIdentifiers = new IExpression[identifiers.Count()];
//Convert the string array into an Expression array
for (int i = 0; i < identifiers.Count(); i++)
expIdentifiers[i] = Expression.String(identifiers[i]);
//Build the query
var query = QueryBuilder.Select(SelectResult.All())
.From(DataSource.Database(db))
.Where(Expression.Property("identifier").In(expIdentifiers));