MongoDB 用MongoTemplate查询指定时间范围的数据


mongoDB大于小于符号对应:

> 大于 $gt
< 小于 $lt
>= 大于等于 $gte
<= 小于等于 $lte

要查询同一个时间多个约束可能出现的error:

org.springframework.data.mongodb.InvalidMongoDbApiUsageException:
Due to limitations of the com.mongodb.BasicDocument, you can't add a second 'createdDate' expression specified as 'createdDate:
Document{{$lt=2018-01-06}}'. Criteria already contains 'createdDate: Document{{$gte=2017-12-31}}'.

解决办法:
要查询同一个字段多个约束需要用andOperator:

Query query = new Query(
Criteria.where("ip").is(ip)
.andOperator(
Criteria.where("createdDate").lt(endDate),
Criteria.where("createdDate").gte(startDate)));



免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM