2012-01-20 47 views

回答

0

我没有设法在MongoDB GORM中搜索Date。我设法做到这一点:

import com.mongodb.* 

class Foo { 
    def fooMethod() { 

     Integer interval = 1 * 1000 * 60 * 60 
     Date date1 = new Date() 
     Date date2 = new Date(date1.getTime() - interval) 

     def mongo = new Mongo("localhost") 
     def db = mongo.getDB('database') 
     DBCollection coll = db.getCollection("collection") 
     DBObject query = QueryBuilder.start().put("dateCreated") 
     .lessThanEquals(date1) 
     .greaterThanEquals(date2) 
     .get() 

     DBCursor cur = coll.find(query) 

     return cur.length() 
    } 
} 
0

Grails为字段域模型生成方法“findBy ...”和“findAllBy ...”。 假设你有域模型。

class Foo { 
    Date date 
} 

Foo.findAllByDateBetween(startDate, endDate)