通过 joda DateTime 范围 scala/casbah 查找 mongodb 条目
我对casbah find 感到困惑。我试图从 MongoDB 中提取 date1 和 date2 之间的所有文档。以下是 mongo 文档的示例集:
{ "_id" : NumberLong("1285248838000"), "openTime" : "Thu Sep 23 2010 06:33:58 GMT-0700 (PDT)", "closeTime" : "Thu Sep 23 2010 06:36:15 GMT-0700 (PDT)", "timeInTrade" : "00:02:17", "direction" : "Long", "size" : 1, "outcome" : "Loss" }
{ "_id" : NumberLong("1285595711000"), "openTime" : "Mon Sep 27 2010 06:55:11 GMT-0700 (PDT)", "closeTime" : "Mon Sep 27 2010 06:57:37 GMT-0700 (PDT)", "timeInTrade" : "00:02:26", "direction" : "Short", "size" : 1, "outcome" : "Win"}
{ "_id" : NumberLong("1285594773000"), "openTime" : "Mon Sep 27 2010 06:39:33 GMT-0700 (PDT)", "closeTime" : "Mon Sep 27 2010 06:41:47 GMT-0700 (PDT)", "timeInTrade" : "00:02:14", "direction" : "Short", "size" : 1, "outcome" : "Win" }
{ "_id" : NumberLong("1286289026000"), "openTime" : "Tue Oct 05 2010 07:30:26 GMT-0700 (PDT)", "closeTime" : "Tue Oct 05 2010 07:36:23 GMT-0700 (PDT)", "timeInTrade" : "00:05:57", "direction" : "Short", "size" : 2, "outcome" : "Loss"}
那么,假设我想从 9 月 27 日起撤回文档。我该如何做呢?
在 casbah 文档中,看起来我可以构造这样的构建器:
val dt = new DateTime("2010-09-27T00:00:00.000-08:00")
val bldr = MongoDBObject.newBuilder
bldr += "openTime" $gte dt $lt dt.plusDays(1)
val result = coll.find(bldr.result)
在我的 IDE (Netbeans) 中,这不会编译,因为“$gte 不是 java.lang.String 的成员”。我使用其他记录的构建过滤器的方法得到了类似的结果。
我怀疑我遇到的下一个问题是它不知道如何比较日期,因为它们存储为 joda DateTimes,所以如果有人有这些问题的经验,我将非常感谢一些指导。
谢谢, John
FOLLOW-UP:
我得到了部分解决方案,但只是因为我使用毫秒作为 _id。这是一些适用于这种情况的代码:
val begin = dt.getMillis
val end = dt.plusDays(1).getMillis
val json = "{ '_id' : { '$gte' : " + begin + " , '$lt' : " + end + "}}"
val dbObject = JSON.parse(json).asInstanceOf[DBObject];
for (x <- coll.find(dbObject)) println(x)
我仍然有兴趣了解适用于 DateTime 而不是 Long millis 的解决方案......
I'm stumped on casbah find. I'm trying to pull back all documents from a MongoDB between date1 and date2. Here's an example set of mongo docs:
{ "_id" : NumberLong("1285248838000"), "openTime" : "Thu Sep 23 2010 06:33:58 GMT-0700 (PDT)", "closeTime" : "Thu Sep 23 2010 06:36:15 GMT-0700 (PDT)", "timeInTrade" : "00:02:17", "direction" : "Long", "size" : 1, "outcome" : "Loss" }
{ "_id" : NumberLong("1285595711000"), "openTime" : "Mon Sep 27 2010 06:55:11 GMT-0700 (PDT)", "closeTime" : "Mon Sep 27 2010 06:57:37 GMT-0700 (PDT)", "timeInTrade" : "00:02:26", "direction" : "Short", "size" : 1, "outcome" : "Win"}
{ "_id" : NumberLong("1285594773000"), "openTime" : "Mon Sep 27 2010 06:39:33 GMT-0700 (PDT)", "closeTime" : "Mon Sep 27 2010 06:41:47 GMT-0700 (PDT)", "timeInTrade" : "00:02:14", "direction" : "Short", "size" : 1, "outcome" : "Win" }
{ "_id" : NumberLong("1286289026000"), "openTime" : "Tue Oct 05 2010 07:30:26 GMT-0700 (PDT)", "closeTime" : "Tue Oct 05 2010 07:36:23 GMT-0700 (PDT)", "timeInTrade" : "00:05:57", "direction" : "Short", "size" : 2, "outcome" : "Loss"}
So, let's say I want to pull back the documents from Sep 27. How would I go about doing that?
In the casbah documentation, it looks like I could construct a builder like this:
val dt = new DateTime("2010-09-27T00:00:00.000-08:00")
val bldr = MongoDBObject.newBuilder
bldr += "openTime" $gte dt $lt dt.plusDays(1)
val result = coll.find(bldr.result)
In my IDE (Netbeans), this will not compile because "$gte is not a member of java.lang.String". I had similar results with the other documented ways to construct my filter.
I suspect that the next problem I would have is that it doesn't know how to compare the dates because they are stored as joda DateTimes, so if anyone has experience with these problems, I would greatly appreciate some guidance.
Thanks,
John
FOLLOW-UP:
I've got a partial solution, but only because I was using the milliseconds as the _id. Here is some code that works for that case:
val begin = dt.getMillis
val end = dt.plusDays(1).getMillis
val json = "{ '_id' : { '$gte' : " + begin + " , '$lt' : " + end + "}}"
val dbObject = JSON.parse(json).asInstanceOf[DBObject];
for (x <- coll.find(dbObject)) println(x)
I'm still interested in learning about a solution that works on DateTime instead of the Long millis...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
opentime
在 Mongo 端存储为字符串。你的 $gte 函数不起作用,b/c 字符串比较不起作用。要实现此目的,您必须使用 $where 子句和正确执行比较的函数。因此,您基本上必须编写一个 javascript 函数来正确解释 JODA 时间。然后,您必须将该函数包含在数据库调用中,否则您必须将其存储在服务器端并从那里继续。
以下是有关 的一些详细信息条款。以下是有关服务器端代码执行的一些详细信息。
opentime
is stored as a string on the Mongo side. Your $gte function won't work b/c string comparison won't work.To make this work, you'll have to use a $where clause and a function that performs the comparison correctly. So you'll basically have to write a javascript function that correctly interprets the JODA time. You'll then have to include that function with your DB call or you'll have to store it server-side and proceed from there.
Here are some details on the where clause. Here are some details on server-side code execution.