【发布时间】:2013-05-23 16:05:30
【问题描述】:
我正在尝试比较以下集合中的日期
$db.notifications.find() { "_id" : ObjectId(“51a4467be4b0142fc8b80eda”),“时间”:“5 月 28 日星期二 11:24:03 IST 2013”,“KEY”:“VALUE1”} {“_id”: ObjectId(“51a4467be4b0142fc8b80edb”),“时间”:“5 月 28 日星期二 11:24:03 IST 2013", "KEY" : "VALUE2" }
使用
$db.notifications.find({ "time" : { "$gt" : "IST 5 月 31 日星期五 00:00:00 2013"}})
但我观察到的是它正在比较将“时间”视为 字符串不是日期,因此第二个命令同时返回我 条目。
$ db.notifications.find({ "time" : { "$gt" : "IST 5 月 31 日星期五 00:00:00 2013"}}) { "_id" : ObjectId("51a4467be4b0142fc8b80eda"), "time" : "星期二 5 月 28 日 11:24:03 IST 2013”,“KEY”:“VALUE1”} {“_id”: ObjectId(“51a4467be4b0142fc8b80edb”),“时间”:“5 月 28 日星期二 11:24:03 IST 2013", "KEY" : "VALUE2" }
能否请您帮助我正确查询并告诉我如何 正确的版本可以用 JAVA 编写。
非常感谢!!
编辑1:
我正在使用以下代码保存日期
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
dateFormat.setLenient(false);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String nowString = dateFormat.format(new Date());
Date nowDate = dateFormat.parse(nowString);
input.put("formattedDate", nowDate);
DBCollection collection = db.getCollection(<collection name>);
DBObject dbObject = (DBObject) JSON.parse(input.toString());
collection.insert(dbObject);"
并使用查询
DBObject query = QueryBuilder.start().put("formattedDate")
.greaterThan(prevDate).get();
DBCursor cursorDocJSON = collection.find(query);
我看到我只将“时间”保存为日期。为什么还是有问题?
【问题讨论】:
-
这样做是因为您将其存储为字符串!