1

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.

Valentin Waeselynck
  • 5,950
  • 26
  • 43
  • See http://stackoverflow.com/questions/24096546/mongoose-populate-vs-object-nesting about performance implications as they will be the same in any language. – DanLebrero Oct 07 '14 at 20:38
  • Mongo only reads from one collection at a time. All populate does is read from one collection, gather a list of referenced IDs, and make a query against another collection for those documents. The data is then joined in memory for your convenience. The thing you have to remember is that each populate is a separate query. Performance starts to go out the window if a single object requires 4 queries... use a relational database at that point. That's what it's really good at. – Ryan Wheale Mar 17 '15 at 18:58

0 Answers0