将JSON添加到mongo数据库中的数组

时间:2016-10-23 15:31:05

标签: clojure monger

我试图使用monger将json地图添加到我的数据库中的数组,但是有些错误。我已经找到了如何在monger文档中执行此操作,但$push$addToSet无法正常工作:

这是我的功能:

(defn add-vehicle [vehicle]
    (let [connect-string (System/getenv "MONGO_CONNECTION")
          {:keys [conn db]} (mg/connect-via-uri connect-string)]
        (mc/update db "drivers-collection" 
                   {:email (:email vehicle)} 
                   {$addToSet {:cars (:vehicle vehicle)}})))

这就是我在nREPL中调用此函数的方式:

(add-vehicle {:email "teste111@hotmail.com" 
              :vehicle {:model "fusca" 
                        :color "rosa" 
                        :plate "AIO-9807"}})

有什么想法吗?

修改

这是drivers-collection中的文档:

{
    "_id": {
        "$oid": "57bee61edcba0f2f7559eb56"
    },
    "email": "teste111@hotmail.com",
    "name": "Guilherme Job",
    "cars": [],
    "customerId": "cus_9O4dhvtuF2926m"
}

我的汽车阵列是空的,我正在尝试添加车辆。

2 个答案:

答案 0 :(得分:1)

不是Mongo的专家,但您可能会从The Clojure Cookbook中找到这个帮助。也有打印版本,我强烈推荐: http://clojure-cookbook.com/

enter image description here

https://github.com/clojure-cookbook/clojure-cookbook/blob/master/06_databases/6-08_mongo.asciidoc

(require '[monger.core :as mongo]
         '[monger.collection :as coll])
(import '[org.bson.types ObjectId])

;; Set the database in the *mongodb-database* var
(mongo/use-db! "mongo-time")

;; Insert one document
(coll/insert "users" {:name "Jeremiah Forthright" :state "TX"})

;; Insert a batch of documents
(coll/insert-batch "users" [{:name "Pete Killibrew" :state "KY"}
                            {:name "Wendy Perkins" :state "OK"}
                            {:name "Steel Whitaker" :state "OK"}
                            {:name "Sarah LaRue" :state "WY"}])

;; Find all documents and return a com.mongodb.DBCursor
(coll/find "users")

;; Find all documents matching a query and return a DBCursor
(coll/find "users" {:state "OK"})

;; Find documents and return them as Clojure maps
(coll/find-maps "users" {:state "OK"})
;; -> ({:_id #<ObjectId 520...>, :state "OK", :name "Wendy Perkins"}
;;     {:_id #<ObjectId 520...>, :state "OK", :name "Steel Whitaker"})

;; Find one document and return a com.mongodb.DBObject
(coll/find-one "users" {:name "Pete Killibrew"})

;; Find one document and return it as a Clojure map
(coll/find-one-as-map "users" {:name "Sarah LaRue"})
;; -> {:_id #<ObjectId 520...>, :state "WY", :name "Sarah LaRue"}

答案 1 :(得分:1)

确保monger.collection/update调用的查询部分包含正确的条件。您还应该检查Mongo驱动程序返回的WriteResult以查看它们是否成功。

我在REPL中使用$push进行了测试,一切正常。如你所见,增加了一辆车:

(require '[monger.core :as mg])
(require '[monger.collection :as mc])
(require '[monger.operators :refer :all])

(def conn (mg/connect))
(def db (mg/get-db conn "test-db"))
(def coll "test-collection")

(mc/insert db coll {:email "a@example.com" :name "Guilherme Job" :cars []})

(mc/find-maps db coll)
;; => ({:_id #object[org.bson.types.ObjectId 0x58f153ea "580e826e7e92729ffb000611"], :email "a@example.com", :name "Guilherme Job", :cars []})

(mc/update db coll {:email" "a@example.com"} {$push {:cars" {:name "Ford"}}})

(mc/find-maps db coll)
;; => ({:_id #object[org.bson.types.ObjectId 0x494168cd "580e826e7e92729ffb000611"], :email "a@example.com", :name "Guilherme Job", :cars [{:name "Ford"}]})