【问题标题】:Spring Mongo criteria query with dates带有日期的 Spring Mongo 条件查询
【发布时间】:2016-10-10 12:06:23
【问题描述】:
我有一个在线和离线日期的产品 pojo
我使用以下查询来获取 online 日期为 Null 或 offline 日期为 Null 的所有产品。但以下查询仅在 both online and offline date are null 时返回我。
query.addCriteria(Criteria.where(Product.IS).is(is).andOperator( Criteria.where(Product.OFFLINE_DATE).is(null).orOperator(Criteria.where(Product.ONLINE_DATE).is(null))));
有什么线索吗?
【问题讨论】:
标签:
mongodb
spring-mongo
spring-mongodb
【解决方案1】:
使用上面的 Query 对象,您正在创建类似于
的 mongo 查询
{ "productId" : "1" , "$and" : [ { "offlineDate" : null , "$or" : [ { "onlineDate" : null }]}]}
你想在哪里查询类似于{ "productId" : "1" , "$and" : [ { "$or" : [ { "offlineDate" : null } , { "onlineDate" : null }]}]}的东西
为orOperator创建一个Criteria对象,放入andOperator:
Criteria orOperator = new Criteria().orOperator(Criteria.where(Product.OFFLINE_DATE).is(null),
Criteria.where(Product.ONLINE_DATE).is(null));
query.addCriteria(Criteria.where(Product.IS).is(is).andOperator(orOperator));
我希望这会有所帮助。