Couchbase parameterized N1QL query IN statement

末鹿安然 提交于 2020-01-24 20:06:03

问题


Using com.couchbase.client, java-client version 2.2.7 I have been unable to get a n1ql query working that uses an IN statement with multiple items see my example query and java code below

public int getCountForDuration(Long startTime, Long endTime, String ids){
    JsonObject placeHolders = JsonObject.create().put("ids", ids).put("startTime", startTime).put("endTime", endTime);
    N1qlQuery query = N1qlQuery.parameterized(COUNT_STATEMENT, placeHolders)            
    N1qlQueryResult result = bucket.query(query);
    ...
}

public static final String COUNT_STATEMENT = "select count(*) as count " +
            "from bucketName " +
            "where docType = 'docId' " +
            "and (id IN [$ids]) " + <----- OFFENDING LINE
            "and publishTimestamp between $startTime and $endTime";

I've tried setting ids using ('), ("), and (`) such as:

ids = "'123', '456'";
ids = "\"123\" , \"456\";
ids = "`123`,`456`"; 

None of these are working when there are multiple ids however if there is only one such as ids = "'123'" it works fine. Also my query works if I use it using CBQ on the terminal.

My question is this how do I crate a parameterized N1QL query which can take multiple items in an IN statement?


回答1:


Removing the brackets around the $ids in the statement and putting the actual ids into placeholders as a JsonArray object should work:

JsonObject placeHolders = JsonObject.create()
    .put("ids", JsonArray.from("id1", "id2", "id3"))
    .put("startTime", startTime)
    .put("endTime", endTime);


来源:https://stackoverflow.com/questions/38711985/couchbase-parameterized-n1ql-query-in-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!