marklogicmarklogic-optic-api

How can Optic api do where in list?


I have the following XQuery code that uses op:from-view. In the op:where section I am trying to select a column that can equal a list of values. When I use one value like op:eq(op:col("client"), $client1) it works fine. However i need it to select ($client1, $client2). How do I do that? I can't seem to find an example anywhere. Thanks.


Solution

  • Optic doesn't have an in() operator at present, so the condition would have to be passed to Optic as

    op:or((op:eq(op:col("client"), $client1), op:eq(op:col("client"), $client2)))
    

    To make that less tedious, one possibility would be to take advantage of build-time expressions as in:

    op:or(
        let $col := op:col("client")
        for $val in ($client1, $client2)
        return op:eq($col, $val)
        )
    

    Or to wrap that expression in a helper function:

    declare function local:in($colName as xs:string, $vals) {
        op:or(
            let $col := op:col($colName)
            for $val in ($vals)
            return op:eq($col, $val)
            )
    };
    

    and use it as follows:

    op:where(
        local:in("client", ($client1, $client2))
        )
    

    Hoping that helps,