1

我正在尝试将 json 映射添加到我的数据库中的数组中,monger但出现了问题。我在 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"
}

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

4

2 回答 2

1

不是 Mongo 方面的专家,但您可能会从 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"}
于 2016-10-24T01:51:58.310 回答
1

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

我已经$push在 REPL 中测试过,一切正常。如您所见,添加了汽车:

(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"}]})
于 2016-10-24T22:05:13.847 回答