1

I'm trying to add a json map to an array in my database using monger but there is something wrong. I've found how to do it in monger documentation, but $push and $addToSet aren't working:

Here is my function:

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

And this is how I'm calling this function in nREPL:

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

Any ideas?

EDIT

This is my document in drivers-collection:

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

My car's array are empty and I'm trying to add a vehicle into it.

guijob
  • 4,413
  • 3
  • 20
  • 39

2 Answers2

1

Not an expert on Mongo, but you may find this helpful from The Clojure Cookbook. Print versions are available as well, which I highly recommend: 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"}
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • Thank you for your answer. Actually, I'm reading Clojure for the Brave and True, and right after this one I'll start Clojure Cookbook based in your recommendation. But, unfortunately, all commands in this book doesn't help me solve my problem. I'm actually looking for something more specific like adding maps directly to an nested array instead of adding into main JSON structure. I've just edited my post so you can read it better – guijob Oct 24 '16 at 02:32
1

Make sure that your query part of monger.collection/update call contains a correct criteria. You should also inspect the WriteResult returned by Mongo driver to see if they were successful or not.

I have tested using $push in REPL and everything works correctly. As you can see a car is added:

(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"}]})
Piotrek Bzdyl
  • 12,965
  • 1
  • 31
  • 49