Introduce monger.collection/remove-by-id

This commit is contained in:
Michael S. Klishin 2012-02-18 19:02:43 +04:00
parent fdcad90cd8
commit 345ee691eb
3 changed files with 21 additions and 3 deletions

View file

@ -55,7 +55,7 @@ monger.collection/find-map-by-id no longer ignore fields argument. Contributed b
`monger.core` will eventually be moved there, but not for 1.0. Contributed by Toby Hede.
### monger.collection/update-by-id
### New convenience functions: monger.collection/update-by-id, /remove-by-id
monger.collection/update-by-id is a new convenience function for updating a single document with
given ObjectId
`monger.collection/update-by-id` is a new convenience function for updating a single document with
given ObjectId. `monger.collection/remove-by-id` is its counterpart for removing documents.

View file

@ -318,6 +318,7 @@
"Update a document with given id"
[^String collection ^ObjectId id ^Map document & { :keys [upsert write-concern] :or { upsert false
write-concern monger.core/*mongodb-write-concern* } }]
(check-not-nil! id "id must not be nil")
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.update coll (to-db-object { :_id id }) (to-db-object document) upsert false write-concern)))
@ -368,6 +369,15 @@
(.remove coll (to-db-object conditions)))))
(defn ^WriteResult remove-by-id
"Removes a single document with given id"
([^String collection ^ObjectId id]
(remove-by-id monger.core/*mongodb-database* collection id))
([^DB db ^String collection ^ObjectId id]
(check-not-nil! id "id must not be nil")
(let [^DBCollection coll (.getCollection db collection)]
(.remove coll (to-db-object { :_id id })))))
;;
;; monger.collection/create-index

View file

@ -62,6 +62,14 @@
(mgcol/remove collection { :language "Clojure" })
(is (= 1 (mgcol/count collection)))))
(deftest remove-a-single-document-from-collection
(let [collection "libraries"
oid (ObjectId.)]
(mgcol/insert-batch collection [{ :language "Clojure" :name "monger" :_id oid }])
(mgcol/remove-by-id collection oid)
(is (= 0 (mgcol/count collection)))
(is (nil? (mgcol/find-by-id oid)))))
;;
;; indexes