【问题标题】:N1ql query to filter by two fields in couchbase databaseN1ql 查询通过 couchbase 数据库中的两个字段进行过滤
【发布时间】:2019-10-28 16:20:50
【问题描述】:

我使用 springboot 和响应式 couchbase 创建了一个 Web 服务,在存储库接口中,我想向 WHERE 子句添加两个字段。我所做的如下

@ViewIndexed(designDoc = "enrolment")
@N1qlPrimaryIndexed
public interface EnrolmentRepository extends ReactiveCouchbaseRepository<Enrolment,String> {
    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and course like $1 and role like $2 within #{#n1ql.bucket}")
    Mono<Enrolment> findEnrolmentByCourseRole(String course, String role);
}

但是当使用上面的代码时,我得到的响应是,

{
   "timestamp": "2019-06-14T03:57:29.670+0000",
   "path": "/enrolments",
   "status": 500,
   "error": "Internal Server Error",
   "message": "Unable to execute n1ql query due to error:{"msg":"syntax error - at role","code":3000}"
}

我的终点是,

http://localhost:8783/enrolments?course=testcourse&role=testrole

【问题讨论】:

    标签: spring-boot reactive-programming spring-webflux n1ql


    【解决方案1】:

    如下更改后效果很好。

    @Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and enrolledCourse = $1 and enrolledRole = $2")
    Mono<Enrolment> findEnrolmentByCourseRole(String enrolledCourse, String enrolledRole);
    

    似乎错误与变量名“角色”有关;到处都改为“enrolledRole”。它工作正常。

    【讨论】:

      【解决方案2】:

      role 是这里描述的保留关键字https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/reservedwords.html

      您可以使用反引号转义标识符

      例子:

       SELECT *
       FROM default
       WHERE `role` = "xyz";
      

      【讨论】:

      • 是的,避免保留关键字使端点正常工作
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多