querying dates from json

15 views
Skip to first unread message

Leandro Oliveira

unread,
Aug 30, 2012, 4:26:16 PM8/30/12
to congomo...@googlegroups.com
Hi all, 

How do I inform congomongo  that post-date is a Date and not a string?

(def query  "{\"post-date\":{\"$gt\":\"2012-08-28T00:00:00Z\"}}")

(cm/fetch :coll 
   :where (json/parse-string query true)})


fetch always returns null.

[ ]s
Leandro.




Tomasz Lipski

unread,
Sep 3, 2012, 1:34:18 PM9/3/12
to congomo...@googlegroups.com
Hi,

First of all - clj-json.core/parse-string (which I assume is referenced as json/parse-string) returns map with strings (not keywords) as keys:

user=> (clj-json.core/parse-string "{\"post-date\":{\"$gt\":\"2012-08-28T00:00:00Z\"}}")
{"post-date" {"$gt" "2012-08-28T00:00:00Z"}}

In such simple case, you can remedy that using e.g. clojure.walk/keywordize-keys:

user=> (clojure.walk/keywordize-keys (clj-json.core/parse-string "{\"post-date\":{\"$gt\":\"2012-08-28T00:00:00Z\"}}"))
{:post-date {:$gt "2012-08-28T00:00:00Z"}}
 
Other thing is that you have to supply java.util.Date object to operate on dates in congomongo, for example:

(somnium.congomongo/fetch :coll :where {:post-date {:$lte (java.util.Date.)}})

To parse a date from string, you could use for example java.text.SimpleDateFormat or Joda Time library and use clojure.walk functions to 
walk the tree parsing any date values based on a regexp or key name.

Anyway such parsing kind of breaks the magic behind passing query as JSON from some external code. Storing date as string should solve this 
particular issue, but would be very inelegant.

--
twitter: @tomeklipski
Reply all
Reply to author
Forward
0 new messages