[JAVA-624] Date range with mongoTemplate gte and lte function is not working on cloudfoundry. Created: 16/Aug/12  Updated: 11/Sep/19  Resolved: 01/Sep/12

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Jitender Saini Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hi! I've inserted data to cloudfoundry(cf) mongodb with the following script.

Expense expense162 = new Expense();
expense162.setExpenseDate(simpleDateFormat.parse("2012-08-01 00:00:00.0"));
mongoTemplate.insert(expense162, "expense");
 
Expense expense163 = new Expense();
expense163.setExpenseDate(simpleDateFormat.parse("2012-08-04 00:00:00.0"));
mongoTemplate.insert(expense163, "expense");
 
Expense expense164 = new Expense();
expense164.setExpenseDate(simpleDateFormat.parse("2012-08-04 00:00:00.0"));
mongoTemplate.insert(expense164, "expense");
 
Expense expense165 = new Expense();
expense165.setExpenseDate(simpleDateFormat.parse("2012-08-04 00:00:00.0"));
mongoTemplate.insert(expense165, "expense");
 
Expense expense166 = new Expense();
expense166.setExpenseDate(simpleDateFormat.parse("2012-08-01 00:00:00.0"));
mongoTemplate.insert(expense166, "expense");
 
Expense expense167 = new Expense();
expense167.setExpenseDate(simpleDateFormat.parse("2012-08-03 00:00:00.0"));
mongoTemplate.insert(expense167, "expense");

Now I'm trying fetching the code with the following java code.

String[] arrMOnthYr = interval.split("-");
Calendar calendar1 = new GregorianCalendar(Integer.parseInt(arrMOnthYr[1]), Integer.parseInt(arrMOnthYr[0])-1, Calendar.DATE);
calendar1.set(Calendar.DATE, calendar1.getActualMinimum(Calendar.DATE));
 
Calendar calendar2 = new GregorianCalendar(Integer.parseInt(arrMOnthYr[1]), Integer.parseInt(arrMOnthYr[0])-1, Calendar.DATE);
calendar2.set(Calendar.DATE, calendar2.getActualMaximum(Calendar.DATE));
List<Expense> list = expenseService.getAllExpense(request.getSession()
.getAttribute("userid").toString(), calendar1.getTime(),
calendar2.getTime());
 
---------------service layer code-----------
 
public List<Expense> getAllExpense(String uid, Date begin, Date end) {
 
System.out.println("Begin---> "+begin);
System.out.println("End---> "+end);
 
Criteria c = new Criteria().andOperator(Criteria.where("expenseDate")
.gte(begin), Criteria.where("expenseDate").lte(end));
c = c.and("userid").is(uid);
 
Query query = new Query(c);
List<Expense> l = mongoTemplate.find(query, Expense.class);
System.out.println("The size of mb is "+l.size());
return l;
}

while I try this on my local mongodb and tomcat server it says.

Begin---> Wed Aug 01 00:00:00 IST 2012
End---> Fri Aug 31 00:00:00 IST 2012
2012-08-16 00:04:11,540{HH:mm:ss} DEBUG [tomcat-http--24] (MongoTemplate.java:1257) - find using query: { "$and" : [ { "expenseDate" : { "$gte" :

{ "$date" : "2012-07-31T18:30:00.000Z"}

}} , { "expenseDate" : { "$lte" :

{ "$date" : "2012-08-30T18:30:00.000Z"}

}}] , "userid" : "5d88cd6f-6f6f-4957-abc2-24f5e2c853ee"} fields: null for class: class com.expense.domains.Expense in collection: expense
The size of mb is 6

But while I try to run the same code on cloudfoundry it prints

Begin---> Wed Aug 01 00:00:00 UTC 2012
End---> Fri Aug 31 00:00:00 UTC 2012
2012-08-15 18:32:44,707{HH:mm:ss} DEBUG [http-11297-1] (MongoTemplate.java:1257) - find using query: { "$and" : [ { "expenseDate" : { "$gte" :

{ "$date" : "2012-08-01T00:00:00.000Z"}

}} , { "expenseDate" : { "$lte" :

{ "$date" : "2012-08-31T00:00:00.000Z"}

}}] , "userid" : "5d88cd6f-6f6f-4957-abc2-24f5e2c853ee"} fields: null for class: class com.expense.domains.Expense in collection: expense
The size of mb is 0

There might be a timezone problem... I don't know.

I've crossed verified that cloudfoundry mongodb and my local mongodb contains the exactly same data.

While I say findAll() for expense on cloudfoundry it returns 6 records, But while I give the same for daterange with gte and lte function the same is not returning any record on cloudfoundry.

Please suggest me solution ASAP since I'm stuck because of this issue.

Thanks

Jitender Saini



 Comments   
Comment by Jeffrey Yemin [ 01/Sep/12 ]

OK, glad you got it working.

Comment by Jitender Saini [ 01/Sep/12 ]

Hi!,

I managed to fixed the issue, Actually It was a problem with mongo service provided by cloudfoundry. Recreating the service on cloudfoundry makes it run flawlessly.

Thanks

Jitender

Comment by Jeffrey Yemin [ 01/Sep/12 ]

Have you been able to re-execute your tests? I'd like to close this issue out.

Comment by Jeffrey Yemin [ 17/Aug/12 ]

I'm not sure what the problem is. Can you execute your tests and attach server logs (with at least -v log level) for both local and cloudfoundry mongod instances?

Comment by Jitender Saini [ 17/Aug/12 ]

Hi! Jeff Yemin,

Thanks for your comment. I just want to highlight here that Please ignore the userid, as userid is very much there in insert script and database. Just because I want to squeeze the code snippet here that's why I didn't mentioned the userid here. The entry is already present in the script e.g. expense162.setUserid("IDValue"); and the same gets insert into database.

Just for your second suggestion that the changes in Criteria replacing the calendar1.getTime(), let me implement and check the same but it doesn't seems it may work. I just want to notify that the same code is working very much on my local workstation with tomcat server, but it showing problem with Cloudfoundry mongodbservice for the date range queries only. Rest of the queries are working very fine on cloudfoundry also e.g. findAll() they are returning proper rows of data. Only date range queries are breaking whereas the same date range queries with the same data are working very much fine on local workstation.

Please check and let me know if you want some more details about code from me.

Thanks

Jitender Saini

Comment by Jeffrey Yemin [ 17/Aug/12 ]

I could see how time zone issues might cause the first and the fifth expense to be filtered out, but not the others.

Your code sample doesn't include anything that sets the userid. Is it possible that the date is not the issue, but rather the userId?

As an aside, and I don't think this will solve your problem, but your query can be more succinctly and efficiently expressed as:

Criteria.where("expenseDate").gte(calendar1.getTime()).lte(calendar2.getTime()).and("userid").is(uid);} 

Generated at Thu Feb 08 08:52:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.