由于BasicDBObject的限制,您不能添加第二个“ $ and”

问题

下面是使用Spring数据和Mongodb的功能,用于查找日期范围内的数据。

public List<RequestAudit> findByIpAndDate(String ip, Date startDate, Date endDate) {

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

}

它遇到以下错误消息:

org.springframework.data.mongodb.InvalidMongoDbApiUsageException: 
	Due to limitations of the com.mongodb.BasicDBObject, you can't add a second '$and' 
	expression specified as '$and : [ { "createdDate" : { "$lt" : { "$date" : "2013-02-25T16:00:00.000Z"}}}]'. 
	Criteria already contains '$and : [ { "createdDate" : { "$gte" : { "$date" : "2013-02-24T16:00:00.000Z"}}}]'

在同一字段“ createdDate ”上添加多个“ $and ”运算符将使Spring将其解释为错误的mongodb查询。 要解决此问题,请将查询更改为:

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

对于同一字段上的多个条件,请使用“逗号”将其组合。

翻译自: https://mkyong.com/java/due-to-limitations-of-the-basicdbobject-you-cant-add-a-second-and/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值