I am accessing Mongo database from Clojure using Monger library. One thing that annoys me is switching back and forth between ObjectId instances and strings.
For example, the this code (mc/find-maps "posts" {})
will evaluate to maps with the value of _id
entry set to instances of ObjectId
class, while in my app I find it more useful to simply have it as a string for which I know that it is unique.
On the other hand for expressions like: (mc/find-map-by-id "posts" (new ObjectId id))
where I do use a String object for the id
parameter, I have to use it to construct an instance of ObjectId
.
Is there a way to make the values of _id
convert between Strings in the application and ObjectId on the mongo side automatically and transparently? Some kind of option that, when enabled, creates maps with string representations of ids, and vice versa converts string representations of ids t object ids when used as parameters in queries?
If not, what other strategies are available?