带数值的 Couchbase 参数化查询

Couchbase parameterized query with numeric values

我正在尝试将数值添加到参数化 AnalyticsQuery,但在查询 运行 时不断出现错误。 java 创建查询如下所示:

  private ParameterizedAnalyticsQuery aggregateQuery(String userId, Long from, Long to) {
    return AnalyticsQuery.parameterized(
        "select d.field1,"
            + " d.field2"
            + " from data d"
            + " where d.userId = $userId"
            + " and d.timestamp between $from and $to",
        JsonObject.create()
            .put("userId", userId)
            .put("from", from)
            .put("to", to)
    );
  }

当查询为运行时,返回以下错误:

<< Encountered \"from\" at column 213. ","code":24000}]

如果我将查询更改为以下内容,那么它将起作用并且 returns 行:

    return AnalyticsQuery.parameterized(
        "select d.field1,"
            + " d.field2"
            + " from data d"
            + " where d.userId = $userId"
            + " and d.timestamp between " + from
            + " and " + to,
        JsonObject.create()
            .put("userId", userId)
    );

为什么当参数不是字符串时会出现问题?有没有办法使用带有数值的参数化查询?

FROM 和 TO 是 N1QL for Analytics 中的保留关键字,因此在用作参数名称时必须放在反引号中:

... and d.timestamp between $`from` and $`to`

有关所有保留关键字的列表,请参阅: https://docs.couchbase.com/server/current/analytics/appendix_1_keywords.html