I'm programming a web backend in Clojure atop a MongoDB database with Monger as a driver.
I really like Monger, but I miss a functionality that I found quite useful in the Mongoose driver : the populate
method that enabled me to emulate elementary joins in my queries.
It went somewhat like this :
MyModel.find()
.populate("myExternalKey")// put the name of the key to populate; Mongoose knows in which collection to look, because you have registered this property as a Ref
.exec();
So first question : is there anything of the like for Monger or Clojure?
I didn't find any, so assuming the answer is no, here is what I plan to do.
I'm thinking of making a Clojure utility function called my-populate
with the following usage :
(my-populate mydoc
{:key1 "aCollectionName"
:key2 {:key3 "anotherCollectionName"
:key4 "yetAnotherCollName"}})
Where mydoc
is a map representing a MongoDB document with external references to other collections at paths (:key1)
, (:key2 :key3)
and (:key2 :key4)
, and calling my-populate
returns the populated document (well, more likely a core.async
channel containing it to tell the truth).
My strategy to do this is simply to make one query per field, which is not hard to do, but now I'm concerned with performance considerations.
Should I be worried about performance issues with this strategy? Is there some MongoDB advanced functionality that would help with it?
Maybe someone who knows what Mongoose's populate
does behind the scene could give me some insights.