2017-04-06 94 views
2

升级到春天MongoDB的数据后1.10.1,运行查询时,像我得到错误:春数据的MongoDB:可选参数@Query不再工作

@Query("{$and :[" 
      + "{ $or : [ { $where: '?0 == null' } , { 'field1' : ?0 } ] }," 
      + "{ $or : [ { $where: '?1 == null' } , { 'field2' : ?1 } ] }," 
      + "]}") 
public Page<Entity> findAll(String param1, String param2) 

检查错误我看到那里里面的参数子句中没有报价且为我结果,我得到:

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 139 and error message 'ReferenceError: test_param_value is not defined :

我看到这里的几个答案,建议的处理可选参数,这种方式((spring-data-mongo - optional query parameters?)),但它不再工作,似乎我无法找到任何东西发布更改日志。

+0

它不知道是什么类型?0或?1,它不能比较undefined == null,因为它不能找出什么是未定义的。希望它是有道理的。 – Euclides

回答

2

在其他人感兴趣的情况下,我设法找到一个解决方法后,检查弹簧数据项目类似ticket int。

看来我在查询中检查空参数的方式并不是一个好的做法。这是来自Spring开发人员的评论:“占位符的设计不是用来组合键/值,而是绑定参数,除此之外,占位符在引用字符串中的使用在转义方面始终存在问题,使用SpEL应该适合您的需求”

我结束了使用SpEL做参数检查,它工作正常。这是它的外观:

@Query("{$and :[" 
     + "?#{ [0] == null ? { $where : 'true'} : { 'field1' : [0] } }," 
     + "?#{ [1] == null ? { $where : 'true'} : { 'field2' : [1] } }," 
     + "]}") 
public Page<Entity> findAll(String param1, String param2, Pageable pageable);