Complex Logical Query

query

#1

I want to select documents which meets this condition:

 (doc.title == "Hello" || doc.title == "World") && (doc.author == "John" || doc.author == "Ken")

How can I perform such query in CBL2.0 ?
I know there’s api for And (IExpression.And) / Or (IExpression.Or)
But I can’t find api for the grouping the condition.

Also, it’s the condition (A or B and C) different from (C and A or B)?
I’m not sure how CBL2.0 handles consecutive logical test.


#2

The Query API is Fluent by design. So you should be able to logically chain together these conditions anyway you like.
So not sure if you attempted that and it failed
Try something like this

            let expr1 = Expression.property("title").equalTo(Expression.string("Hello")).or(Expression.property("title").equalTo(Expression.string("world")))
            let expr2 = Expression.property("author").equalTo(Expression.string("john")).or(Expression.property("author").equalTo(Expression.string("ken")))
            
            let expr = expr1.and(expr2)


#3

It didn’t occur to me that the Expression itself is indeed a group.
Now I know how to do it.

Thanks for the information.