Implement monger.collection/save
This commit is contained in:
parent
e896a5dfc3
commit
861be36c54
2 changed files with 41 additions and 13 deletions
|
|
@ -19,21 +19,21 @@
|
||||||
;; monger.collection/insert
|
;; monger.collection/insert
|
||||||
|
|
||||||
(defn ^WriteResult insert
|
(defn ^WriteResult insert
|
||||||
([^String collection, ^DBObject doc]
|
([^String collection, ^DBObject document]
|
||||||
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
(.insert coll (to-db-object doc) monger.core/*mongodb-write-concern*)))
|
(.insert coll (to-db-object document) monger.core/*mongodb-write-concern*)))
|
||||||
([^String collection, ^DBObject doc, ^WriteConcern concern]
|
([^String collection, ^DBObject document, ^WriteConcern concern]
|
||||||
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
(.insert coll (to-db-object doc) concern))))
|
(.insert coll (to-db-object document) concern))))
|
||||||
|
|
||||||
|
|
||||||
(defn ^WriteResult insert-batch
|
(defn ^WriteResult insert-batch
|
||||||
([^String collection, ^List docs]
|
([^String collection, ^List documents]
|
||||||
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
(.insert coll (to-db-object docs) WriteConcern/NORMAL)))
|
(.insert coll (to-db-object documents) WriteConcern/NORMAL)))
|
||||||
([^String collection, ^List docs, ^WriteConcern concern]
|
([^String collection, ^List documents, ^WriteConcern concern]
|
||||||
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
(.insert coll (to-db-object docs) concern))))
|
(.insert coll (to-db-object documents) concern))))
|
||||||
|
|
||||||
;; monger.collection/find
|
;; monger.collection/find
|
||||||
(declare fields-to-db-object)
|
(declare fields-to-db-object)
|
||||||
|
|
@ -59,8 +59,7 @@
|
||||||
([^String collection, ^String id, ^List fields]
|
([^String collection, ^String id, ^List fields]
|
||||||
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)
|
(let [#^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)
|
||||||
map-of-fields (fields-to-db-object fields)]
|
map-of-fields (fields-to-db-object fields)]
|
||||||
(.findOne coll (to-db-object { :_id id }) (to-db-object map-of-fields))))
|
(.findOne coll (to-db-object { :_id id }) (to-db-object map-of-fields)))))
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -83,6 +82,17 @@
|
||||||
(.update coll (to-db-object conditions) (to-db-object document) upsert multi write-concern)))
|
(.update coll (to-db-object conditions) (to-db-object document) upsert multi write-concern)))
|
||||||
|
|
||||||
|
|
||||||
|
;; monger.collection/save
|
||||||
|
|
||||||
|
(defn ^WriteResult save
|
||||||
|
([^String collection, ^Map document]
|
||||||
|
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
|
(.save coll (to-db-object document) monger.core/*mongodb-write-concern*)))
|
||||||
|
([^String collection, ^Map document, ^WriteConcern write-concern]
|
||||||
|
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
|
(.save coll document write-concern))))
|
||||||
|
|
||||||
|
|
||||||
;; monger.collection/update-multi
|
;; monger.collection/update-multi
|
||||||
;; monger.collection/remove
|
;; monger.collection/remove
|
||||||
|
|
||||||
|
|
@ -92,8 +102,7 @@
|
||||||
(.remove coll (to-db-object {}))))
|
(.remove coll (to-db-object {}))))
|
||||||
([^String collection, ^Map conditions]
|
([^String collection, ^Map conditions]
|
||||||
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
|
||||||
(.remove coll (to-db-object conditions))))
|
(.remove coll (to-db-object conditions)))))
|
||||||
)
|
|
||||||
|
|
||||||
;; monger.collection/ensure-index
|
;; monger.collection/ensure-index
|
||||||
;; monger.collection/drop-index
|
;; monger.collection/drop-index
|
||||||
|
|
|
||||||
|
|
@ -201,3 +201,22 @@
|
||||||
(is (= 0 (monger.collection/count collection { :language "Clojure" })))
|
(is (= 0 (monger.collection/count collection { :language "Clojure" })))
|
||||||
(is (= 1 (monger.collection/count collection { :language "Scala" })))
|
(is (= 1 (monger.collection/count collection { :language "Scala" })))
|
||||||
(is (= 3 (monger.collection/count collection { :language "Python" })))))
|
(is (= 3 (monger.collection/count collection { :language "Python" })))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest save-a-new-document
|
||||||
|
(let [collection "people"
|
||||||
|
document { :name "Joe", :age 30 }]
|
||||||
|
(monger.collection/remove collection)
|
||||||
|
(is (monger.errors/ok? (monger.collection/save "people" document)))
|
||||||
|
(is (= 1 (monger.collection/count collection)))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest update-an-existing-document-using-save
|
||||||
|
(let [collection "people"
|
||||||
|
doc-id "people-1"
|
||||||
|
document { :_id doc-id, :name "Joe", :age 30 }]
|
||||||
|
(monger.collection/remove collection)
|
||||||
|
(is (monger.errors/ok? (monger.collection/insert "people" document)))
|
||||||
|
(is (= 1 (monger.collection/count collection)))
|
||||||
|
(monger.collection/save collection { :_id doc-id, :name "Alan", :age 40 })
|
||||||
|
(is (= 1 (monger.collection/count collection { :name "Alan", :age 40 })))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue