guijob
guijob

Reputation: 4488

Adding JSON to an array in mongo database

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 "[email protected]" 
              :vehicle {:model "fusca" 
                        :color "rosa" 
                        :plate "AIO-9807"}})

Any ideas?

EDIT

This is my document in drivers-collection:

{
    "_id": {
        "$oid": "57bee61edcba0f2f7559eb56"
    },
    "email": "[email protected]",
    "name": "Guilherme Job",
    "cars": [],
    "customerId": "cus_9O4dhvtuF2926m"
}

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

Upvotes: 1

Views: 267

Answers (2)

Piotrek Bzdyl
Piotrek Bzdyl

Reputation: 13185

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 "[email protected]" :name "Guilherme Job" :cars []})

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

(mc/update db coll {:email" "[email protected]"} {$push {:cars" {:name "Ford"}}})

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

Upvotes: 1

Alan Thompson
Alan Thompson

Reputation: 29974

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

Upvotes: 1

Related Questions