An Architect's View

CFML, Clojure, Software Design, Frameworks and more...

An Architect's View

CongoMongo & Clojure 1.3.0

June 10, 2011 ·

CongoMongo is a Clojure wrapper around the MongoDB Java driver. It makes it pretty easy to interact with MongoDB:

(use 'somnium.congomongo)
;; connect to "example" database via mongo1:27017 or
;; mongo2:25017 seed hosts (I have a replica set running)
(def db (make-connection :example
    { :host "mongo1" } { :host "mongo2" :port 25017 } ))
;; insert a document into the "clojure" collection:
(with-mongo db
  (insert! :clojure
    {:name "Clojure" :tagline "The language from the future."}))
;; fetch one document from the "clojure" collection:
(with-mongo db (fetch-one :clojure))
;; you'll get back something like:
{:_id #<ObjectId 4df296940c2851a1d360ec10>,
  :name "Clojure",
  :tagline "The language from the future."}

Unfortunately for me, CongoMongo only worked with Clojure 1.2.1 due to dependencies on Clojure Contrib 1.2.0 - which won't run on Clojure 1.3.0. At World Singles we're relying on quite a few things in the alpha releases of "Clojure.Next" so, after talking to the CongoMongo team, I set about making it compatible with Clojure 1.3.0-alpha8. If you're using Clojure 1.3.0 and want to work with MongoDB, you should be able to clone the github repo for CongoMongo and run lein install to put a JAR into your local maven repository. Then you can add [congomongo "0.1.5-SNAPSHOT"] to your project's project.clj and you should be good to go! (Updated 6/29: CongoMongo has been updated on Clojars so you can get the version with 1.3.0 compatibility more easily)

Tags: clojure · mongodb

0 responses