English 中文(简体)
使用 Karras 从 Mongo 收藏获取文档
原标题:Fetching Documents from Mongo Collection using Karras

假设我在蒙戈布有收藏品,它的所有文件都采用以下结构。

{
    "_id":5,
    "key-name":"test",
    "meta-data":{
        "user-id":2,
        "status":2
    }
}

假设我想找到所有收藏的“ 坚固” “ 用户代号” = 2 < / 坚固” 。 我可以用以下函数很容易地通过 mongo 贝壳 找到它 。

db.mycol.find({"meta-data.user-id" : 2})

我需要通过Churjure检索这些文件。 因此,我有以下一套功能。

这是我至今为止的守则

(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}))

retreve-doc 是我要用来获取文档的函数。 下列函数执行此任务 。

(fetch (job-collection) {"meta-data.user-id" id})

而这就是你如何用 https://github.com/wilkes/karras" rel="nolfollow"来解决这个问题 Karras Karras

最佳回答

答复:

(fetch (job-collection) {"meta-data.user-id" id})
问题回答

尝试此 :

(fetch (job-collection) {:meta-data {:user-id id}})




相关问题
How to improve Clojures error messages

I ve been playing a bit with Clojure and so far is fairly impressed, but one thing that I keep running into is wierd error messages from Clojure. This comes in two forms: Java errors, like null ...

clojure rmi classpath problem

I am trying to use clojure to implement a "plugin" for some vendor supplied software. Here is a little background on the vendor supplied software. It expects me to implement a particular interface ...

Help translating this Java codeblock to Clojure?

I m getting my feet wet with Clojure, and trying to get used to functional programming. I ve been translating various imperative functions from other languages into their Clojure equivalents -- and ...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand s suggestion. (defn redux [[current next] flag] [(if flag ...

taking java method names as function arg in clojure

All, I want to create a function that takes a symbol representing a java method and applies it to some object: (user=> (defn f [m] (. "foo" (m))) When I execute this, I get a result much ...

how to efficiently apply a medium-weight function in parallel

I m looking to map a modestly-expensive function onto a large lazy seq in parallel. pmap is great but i m loosing to much to context switching. I think I need to increase the size of the chunk of work ...