Adding some docs and tests to collection.

This commit is contained in:
Oleksandr Petrov 2011-10-11 18:55:40 +02:00
parent dd4bfe03c6
commit 75d52331bf
2 changed files with 54 additions and 2 deletions

View file

@ -22,6 +22,7 @@
;; ;;
(defn ^WriteResult insert (defn ^WriteResult insert
"Saves document to database"
([^String collection, ^DBObject document] ([^String collection, ^DBObject document]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert ^DBCollection coll ^DBObject (to-db-object document) ^WriteConcern monger.core/*mongodb-write-concern*))) (.insert ^DBCollection coll ^DBObject (to-db-object document) ^WriteConcern monger.core/*mongodb-write-concern*)))
@ -31,9 +32,10 @@
(defn ^WriteResult insert-batch (defn ^WriteResult insert-batch
"Saves documents do database"
([^String collection, ^List documents] ([^String collection, ^List documents]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern WriteConcern/NORMAL))) (.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern monger.core/*mongodb-write-concern*)))
([^String collection, ^List documents, ^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 ^DBCollection coll ^List (to-db-object documents) ^WriteConcern concern)))) (.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern concern))))
@ -44,6 +46,7 @@
(declare fields-to-db-object) (declare fields-to-db-object)
(defn ^DBCursor find (defn ^DBCursor find
"Queries for an object in this collection."
([^String collection] ([^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.find coll))) (.find coll)))
@ -76,6 +79,7 @@
;; ;;
(defn ^DBObject find-one (defn ^DBObject find-one
"Returns a single object from this collection matching the query."
([^String collection, ^Map ref] ([^String collection, ^Map ref]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.findOne ^DBCollection coll ^DBObject (to-db-object ref)))) (.findOne ^DBCollection coll ^DBObject (to-db-object ref))))
@ -117,6 +121,7 @@
;; monger.collection/group ;; monger.collection/group
;; ;;
;; TBD ;; TBD
@ -124,6 +129,13 @@
;; monger.collection/count ;; monger.collection/count
;; ;;
(defn ^long count (defn ^long count
"Returns the number of documents in this collection.
Takes optional conditions as an argument.
(monger.collection/count collection)
(monger.collection/count collection { :first_name \"Paul\" })"
([^String collection] ([^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.count coll))) (.count coll)))

View file

@ -330,6 +330,46 @@
(is (= (:language doc) "Clojure")))) (is (= (:language doc) "Clojure"))))
(is (empty? (mgcol/find collection { :language "Erlang" } [:name])))))) (is (empty? (mgcol/find collection { :language "Erlang" } [:name]))))))
;; more sophisticated examples
(deftest find-with-conditional-operators-comparison
(let [collection "libraries"]
(monger.collection/insert-batch collection [{ :language "Clojure", :name "monger" :users 1}
{ :language "Clojure", :name "langohr" :users 5 }
{ :language "Clojure", :name "incanter" :users 15 }
{ :language "Scala", :name "akka" :users 150}])
(is (= 2 (.count (monger.collection/find collection { :users { "$gt" 10 }}))))
(is (= 3 (.count (monger.collection/find collection { :users { "$gte" 5 }}))))
(is (= 2 (.count (monger.collection/find collection { :users { "$lt" 10 }}))))
(is (= 2 (.count (monger.collection/find collection { :users { "$lte" 5 }}))))
(is (= 1 (.count (monger.collection/find collection { :users { "$gt" 10 "$lt" 150 }}))))
))
(deftest find-on-embedded-arrays
(let [collection "libraries"]
(monger.collection/insert-batch collection [{ :language "Clojure", :tags [ "functional" ] }
{ :language "Scala", :tags [ "functional" "object-oriented" ] }
{ :language "Ruby", :tags [ "object-oriented" "dynamic" ] }])
(is (= "Scala" (:language (first (monger.collection/find-maps collection { :tags { "$all" [ "functional" "object-oriented" ] } } )))))
(is (= 3 (.count (monger.collection/find-maps collection { :tags { "$in" [ "functional" "object-oriented" ] } } ))))))
(deftest find-with-conditional-operators-on-embedded-documents
(let [collection "people"]
(monger.collection/insert-batch collection [{ :name "Bob", :comments [ { :text "Nice!" :rating 1 }
{ :text "Love it" :rating 4 }
{ :text "What?":rating -5 } ] }
{ :name "Alice", :comments [ { :text "Yeah" :rating 2 }
{ :text "Doh" :rating 1 }
{ :text "Agreed" :rating 3 }
] } ])
(is (= 1 (.count (monger.collection/find collection { :comments { "$elemMatch" { :text "Nice!" :rating { "$gte" 1 } } } }))))
(is (= 2 (.count (monger.collection/find collection { "comments.rating" 1 } ))))
(is (= 1 (.count (monger.collection/find collection { "comments.rating" { "$gt" 3 } }))))))
;; ;;
;; update, save ;; update, save