diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 47a7b97..be600b1 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -22,6 +22,7 @@ ;; (defn ^WriteResult insert + "Saves document to database" ([^String collection, ^DBObject document] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert ^DBCollection coll ^DBObject (to-db-object document) ^WriteConcern monger.core/*mongodb-write-concern*))) @@ -31,9 +32,10 @@ (defn ^WriteResult insert-batch + "Saves documents do database" ([^String collection, ^List documents] (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] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.insert ^DBCollection coll ^List (to-db-object documents) ^WriteConcern concern)))) @@ -44,6 +46,7 @@ (declare fields-to-db-object) (defn ^DBCursor find + "Queries for an object in this collection." ([^String collection] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.find coll))) @@ -76,6 +79,7 @@ ;; (defn ^DBObject find-one + "Returns a single object from this collection matching the query." ([^String collection, ^Map ref] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.findOne ^DBCollection coll ^DBObject (to-db-object ref)))) @@ -117,6 +121,7 @@ ;; monger.collection/group ;; + ;; TBD @@ -124,6 +129,13 @@ ;; monger.collection/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] (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.count coll))) @@ -275,4 +287,4 @@ (defn- fields-to-db-object [^List fields] - (zipmap fields (repeat 1))) \ No newline at end of file + (zipmap fields (repeat 1))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c17b094..205a90d 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -330,6 +330,46 @@ (is (= (:language doc) "Clojure")))) (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