【发布时间】:2014-11-25 21:32:22
【问题描述】:
这是我第一次使用带有 java 驱动程序的 Mongodb。我可以使用 javascript 和 Date() 对象通过命令行查询数据库,但是,我在使用驱动程序时遇到了问题。根据我的查询,任何人都可以看到问题所在吗?谢谢
Date current = new Date();
DBCollection coll = db.getCollection("messages");
BasicDBObject query = new BasicDBObject("created_on", new BasicDBObject("$gte", new Date(current.getYear(), current.getMonth(), current.getDate())).
append("created_on", new BasicDBObject("$lt", new Date(current.getYear(), current.getMonth() - 1, current.getDate()))));
System.out.println("Query: " + query);
DBCursor cursor = coll.find(query);
查询:{“created_on”:{“$gte”:{“$date”: “2012-12-06T05:00:00.000Z”},“created_on”:{“$lt”:{“$date”: “2012-11-06T05:00:00.000Z”}}}}
附:如果不是很明显,我正在尝试查找上个月内的所有记录。
【问题讨论】: