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(
	return mongoOperation.find(query, RequestAudit.class);

It hits following error message : 
	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"}}}]'


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(

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

Tags :
Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Related Posts

Popular Posts