Note that there are some explanatory texts on larger screens.

plurals
  1. POfinding mongodb entries by joda DateTime range scala/casbah
    primarykey
    data
    text
    <p>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:</p> <pre><code>{ "_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"} </code></pre> <p>So, let's say I want to pull back the documents from Sep 27. How would I go about doing that? </p> <p>In the casbah documentation, it looks like I could construct a builder like this:</p> <pre><code>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) </code></pre> <p>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. </p> <p>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.</p> <p>Thanks, John</p> <p>FOLLOW-UP:</p> <p>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:</p> <pre><code>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 &lt;- coll.find(dbObject)) println(x) </code></pre> <p>I'm still interested in learning about a solution that works on DateTime instead of the Long millis...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload