我希望在查询注释中使用 MongoRepository 来使用正则表达式。到目前为止我找到的唯一信息是一个中文帖子,但没有解释它是如何工作的,我不确定是否是我要找的。p>
@Query("{ 'name':{'$regex':?2,'$options':'i'}, sales': {'$gte':?1,'$lte':?2 }}") public Page findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);
有人知道是否可以在查询中使用特定的正则表达式吗?
问候。
最佳答案
i toggles case insensitivity, and allows all letters in the pattern to match upper and lower cases.
在 MongoDB docs 中阅读有关 $regex 的更多信息.
在您的查询中,您似乎混淆了参数顺序(名称正则表达式中的?2)。我认为应该是:
@Query("{ 'name':{$regex:?0,$options:'i'}, sales': {$gte:?1,$lte:?2}}")
public Page findByNameAndAgeRange(String name,double ageFrom,double ageTo,Pageable page);
关于java - spring MongoRepository 查询正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16729728/