Reputation: 183
Let's assume that I have a collection in mongodb, where all of its documents take the following structure.
{
"_id":5,
"key-name":"test",
"meta-data":{
"user-id":2,
"status":2
}
}
Let assume I want to find all collections where "user-id" = 2. I can do this easily Via the mongo shell using following function.
db.mycol.find({"meta-data.user-id" : 2})
I need to retrieve these documents via clojure. Therefore, I have the following set of functions.
Following are my code so far I have.
(ns demo.repository
(:use karras.core)
(:use karras.sugar)
(:use karras.collection))
(def mongo-connection (atom nil))
(def mandate-db (atom nil))
(defn mongo-connect []
(if (nil? @mongo-connection)
(do
(swap! mongo-connection (constantly (connect "192.168.0.6" 27017)))
(swap! mandate-db (constantly (mongo-db @mongo-connection "mydb"))))))
(defn mongo-close []
(if-not (nil? @mongo-connect)
(.close @mongo-connect)))
(defn- job-collection [] (collection @mandate-db "mycol"))
(defn retrieve-doc [id]
(fetch (job-collection) {"meta-data.user-id" id}))
retrieve-doc is the function I intent to use to fetch documents. The following function does the job.
(fetch (job-collection) {"meta-data.user-id" id})
And this is how you can solve this with Karras
Upvotes: 2
Views: 210