6

I am new to clojure.

Are there any idioms/patterns around connecting to mongodb through monger?

Do I have to connect and disconnect using

(monger.core/connect) & (monger.core/disconnect conn)

respectively. each time ?

Is there a way I can reuse a connection from a connection pool?

Aneesh
  • 928
  • 10
  • 20

2 Answers2

7

monger uses MongoClient, which does connection pooling. After you connect, you can keep working with that pool until you're done, and then disconnect. See the monger documentation for supported connection options (e.g. maximum number of connections in the pool, default 10).

Diego Basch
  • 12,764
  • 2
  • 29
  • 24
  • 1
    Sorry I should have been more clear. This is a web application. When you say keep working with that pool until you are done, would each request have to connect and disconnect? – Aneesh May 30 '14 at 02:33
  • 3
    No, you want to call connect before you launch the app (jetty, I assume). Query the db from your routes, no need to connect every time. – Diego Basch May 30 '14 at 05:20
  • 1
    How do I get back the connection in subsequent requests, since the (monger.collection/insert), (monger.collection/find-one-as-map) functions seem to require a db as a parameter and the (monger.core/get-db) function requires a connection as a parameter? – Aneesh Jun 04 '14 at 01:27
  • 6
    When you connect, put the connection in an atom so you can see it from all subsequent calling threads. – Diego Basch Jun 04 '14 at 21:57
  • 1
    very helpful comments and answer, thanks everyone for being so friendly and respectful and insightful ^_^ – sova Feb 09 '15 at 22:10
  • I realise this is an old thread but it seems still relevant. I have one further question: do I need to worry about concurrency or can I just share the same connection object among all my threads and let the connection pool worry about allocating physical connections? – Andy Sep 18 '22 at 09:46
2

We just need example code, right? The documentation only talks about creating locally bound connections through let, so this will get you started:

(ns pipegen.core
    (:require [monger.core :as mg]
              [monger.collection :as mc]))

(def conn (atom (mg/connect-via-uri mongo-uri)))
(mc/insert (:db @conn) "collectionname" {:name "methuzula" :age 123})

It wasn't clear to me, from documentation, the return type of mg/connect-via-uri, so I'll mention briefly in case it helps someone:

{:conn #object[com.mongodb.MongoClient]
 :db   #object[com.mongodb.DB]}

If you save that to a def, you can reuse it as needed.


Side Notes, less related to your question, but will probably help you:

This may not be the best design pattern for connecting to dbs. Here are some design patterns for dependency injection I enjoyed reading through, it gives some better ideas of passing around the mongo connection context, the Reader Monad seems particularly cool, I'll have to try it out myself! http://software-ninja-ninja.blogspot.co.il/2014/04/5-faces-of-dependency-injection-in.html

Also, clojurians on slack is a friendly community I didn't find out until after many a headache with clojure, check em out! https://clojurians.slack.com/

Josh.F
  • 3,666
  • 2
  • 27
  • 37