Due to limitations of the BasicDBObject, you cant add a second $and

摘要: Using Spring data and Mongodb, below is a function to find data within a date range.

Problem

Using Spring data and Mongodb, below is a function to find data within a date range.

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);

It hits following error message :

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"}}}]'

Solution

Adding multiple “$and” operators on the same field “createdDate” will make Spring interprets it into a wrong mongodb query. To fix it, change the query to follow :

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

For multiple criteria on the same field, uses a “comma” to combine them.

上一篇: Maven, JAVA_HOME is not defined correctly on Mac OSX
下一篇: How to Set $JAVA_HOME environment variable on Mac OS X
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号