Merge pull request #54 from ikitommi/setOnInsert

$setOnInsert operator
This commit is contained in:
Michael Klishin 2013-06-19 21:49:24 -07:00
commit 7cc7b3a2d5
2 changed files with 26 additions and 0 deletions

View file

@ -125,6 +125,13 @@
;; (monger.collection/update "things" { :_id oid } { $unset { :weight 1 } }) ;; (monger.collection/update "things" { :_id oid } { $unset { :weight 1 } })
(defoperator $unset) (defoperator $unset)
;; $setOnInsert assigns values to fields during an upsert only when using the upsert option to the update operation performs an insert.
;; New in version 2.4. http://docs.mongodb.org/manual/reference/operator/setOnInsert/
;;
;; EXAMPLES:
;; (monger.collection/find-and-modify "things" {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
(defoperator $setOnInsert)
;; $rename renames a given field ;; $rename renames a given field
;; ;;
;; EXAMPLES: ;; EXAMPLES:

View file

@ -114,6 +114,25 @@
(is (mgres/ok? (mgcol/update coll { :_id oid } { $unset { :published 1 :featured true } }))) (is (mgres/ok? (mgcol/update coll { :_id oid } { $unset { :published 1 :featured true } })))
(is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid))))) (is (= { :_id oid :title "Document 1" } (mgcol/find-map-by-id coll oid)))))
;;
;; $setOnInsert
;;
(deftest setOnInsert-in-upsert-for-non-existing-document
(let [coll "docs"
now 456
oid (ObjectId.)]
(mgcol/find-and-modify coll {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
(is (= { :_id oid :lastseen now :firstseen now} (mgcol/find-map-by-id coll oid)))))
(deftest setOnInsert-in-upsert-for-existing-document
(let [coll "docs"
before 123
now 456
oid (ObjectId.)]
(mgcol/insert coll { :_id oid :firstseen before :lastseen before})
(mgcol/find-and-modify coll {:_id oid} {$set {:lastseen now} $setOnInsert {:firstseen now}} :upsert true)
(is (= { :_id oid :lastseen now :firstseen before} (mgcol/find-map-by-id coll oid)))))
;; ;;
;; $push ;; $push