1

I'm trying to get all documents between two dates using groovy. I've tried several approaches and haven't been successful. Most lately I've tried the approach listed here Java/MongoDB query by date

Here is my code :

BasicDBObject query = new BasicDBObject("lastDate", //
    new BasicDBObject("$gte",start).append("$lt", end));
    def temp=  getDB().sysLog.find(query)

However I get the error :

     No such property: gte for class: dev.ReportController Possible solutions: DB

I can't figure out why this approach will not work. It looks as if groovy handles the "$gte" differently then Java which makes sense but I'm not sure how I should format the request.

Community
  • 1
  • 1
Travis
  • 705
  • 6
  • 30

1 Answers1

4

You need to use single quotes

new BasicDBObject( '$gte', start ).append( '$lt', end ) )

Using double quotes, Groovy tries to expand the gte into a Groovy String, but as it says, you have no variable called gte

Indeed, you should be able to convert you code to the more groovy:

def temp = getDB().sysLog.find( [ lastDate:[ '$gte': start, '$lt': end ] ] as BasicDBObject )
tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thank you..can't believe I missed that..DOes the date have to be a certain format/type? At the moment I'm using epoch time but am not getting anything back freom the db even though "start" and "end" have multiple dates between them? – Travis May 20 '13 at 13:57
  • I think it can just be a `java.util.Date` object – tim_yates May 20 '13 at 13:59