我有一个查询,有多个参数是可选的。
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter}" +
"#{(#id != null) ? ' AND META().id = $id' : ''}" +
"#{(#name != null) ? ' AND name like $name' : ''}" +
"#{(#time != null) ? ' AND time = $time' : ''}" +
"#{(#x == null) ? '' : ' AND x = $x' }" +
)
org.springframework.data.domain.ObjectDRP<OjectDRP> findAllAdvanced(
@Param("id") String id,
@Param("name") String name,
@Param("time") Boolean time,
@Param("x") String x
我有这个网址。http:/localhost:4200apiobjectDRP?page=0&size=10&x=test&sort=lastModifiedDate,desc。
服务器给我。
{"msg":"Error evaluating filter. - cause: No value for named parameter $x.","code":5010}
但如果我把查询改为:
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter}" +
"AND $x is NOT NULL"
"#{(#id != null) ? ' AND META().id = $id' : ''}" +
"#{(#name != null) ? ' AND name like $name' : ''}" +
"#{(#time != null) ? ' AND time = $time' : ''}" +
"#{(#x == null) ? '' : ' AND x = $x' }" +
)
它将对每一组参数都有效: name, time, x; name, time; time... 等等。这是我第一次用 n1ql
和 spel
所以我不是很明白发生了什么。谁能给我一些帮助,解决这个问题。
2.在n1ql查询中,对于一个null的必要参数该怎么做?
E.Q:
"SELECT COUNT(*) AS count FROM test_pages WHERE _class = "com.test.testM.test.example.example" AND test = null"
一旦你使用了命名位置参数,你必须在执行过程中传递它们,否则在执行过程中,如果那部分谓词被执行,查询将返回错误。
在SQL比较中,NULL表示未定义,NULL总是假的(即x == NULL,未定义不能与任何值比较)。如果你的查询需要这样的比较,必须使用
x IS MISSING
x IS NOT MISSING
x IS NULL
x IS NOT NULL
x IS VALUED
x IS NOT VALUED
你也可以使用下面的东西
x = IFMISSINGORNULL($x,"")
https:/docs.couchbase.comservercurrentn1qln1ql-language-referencecomparisonops.html。
正确的查询将是
SELECT COUNT(1) AS count
FROM `testM`
WHERE _class = "com.test.testL.example.example"
AND test IS NULL;