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

Adding JSON to an array in mongo database

我正在尝试使用 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"
}

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

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

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"}

确保 monger.collection/update 调用的 query 部分包含正确的条件。您还应该检查 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"}]})