diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 19be291..d571104 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -19,14 +19,28 @@ ;; THE SOFTWARE. (ns monger.collection - (:import (com.mongodb Mongo DB)) - ) + (:import (com.mongodb Mongo DB WriteResult DBObject WriteConcern) (java.util List)) + (:require [monger core convertion errors])) ;; ;; API ;; ;; monger.collection/insert + +(defn ^WriteResult insert + ([^DB db, ^String collection, ^DBObject doc] + (.insert (.getCollection db collection) (monger.convertion/to-db-object doc) WriteConcern/NORMAL)) + ([^DB db, ^String collection, ^DBObject doc, ^WriteConcern concern] + (.insert (.getCollection db collection) (monger.convertion/to-db-object doc) concern))) + + +(defn ^WriteResult insert-batch + ([^DB db, ^String collection, ^List docs] + (.insert (.getCollection db collection) (monger.convertion/to-db-object docs) WriteConcern/NORMAL)) + ([^DB db, ^String collection, ^List docs, ^WriteConcern concern] + (.insert (.getCollection db collection) (monger.convertion/to-db-object docs) concern))) + ;; monger.collection/find ;; monger.collection/group @@ -39,5 +53,12 @@ ;; monger.collection/update-multi ;; monger.collection/remove +(defn ^WriteResult remove +([^DB db, ^String collection] + (.remove (.getCollection db collection) (monger.convertion/to-db-object {}))) + ([^DB db, ^String collection, ^DBObject conditions] + (.remove (.getCollection db collection) (monger.convertion/to-db-object conditions))) + ) + ;; monger.collection/ensure-index ;; monger.collection/drop-index \ No newline at end of file diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index 04b6e9e..b2534f3 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -1,7 +1,65 @@ +(set! *warn-on-reflection* true) + (ns monger.test.collection - (:require [monger core collection]) + (:import [com.mongodb WriteResult WriteConcern]) + (:require [monger core collection errors] [clojure stacktrace]) (:use [clojure.test])) + +;; +;; insert +;; + +(deftest insert-a-basic-document-without-id-and-with-default-write-concern + (let [connection (monger.core/connect) + db (monger.core/get-db connection "monger-test") + collection "people" + doc { :name "Joe", :age 30 }] + (monger.collection/remove db collection) + (is (monger.errors/ok? (monger.collection/insert db "people" doc))) + (is (= 1 (monger.collection/count db collection))))) + + +(deftest insert-a-basic-document-without-id-and-with-explicit-write-concern + (let [connection (monger.core/connect) + db (monger.core/get-db connection "monger-test") + collection "people" + doc { :name "Joe", :age 30 }] + (monger.collection/remove db collection) + (is (monger.errors/ok? (monger.collection/insert db "people" doc WriteConcern/SAFE))) + (is (= 1 (monger.collection/count db collection))))) + + + +;; +;; insert +;; + +(deftest insert-a-batch-of-basic-documents-without-ids-and-with-default-write-concern + (let [connection (monger.core/connect) + db (monger.core/get-db connection "monger-test") + collection "people" + docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]] + (monger.collection/remove db collection) + (is (monger.errors/ok? (monger.collection/insert-batch db "people" docs))) + (is (= 2 (monger.collection/count db collection))))) + +(deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern + (let [connection (monger.core/connect) + db (monger.core/get-db connection "monger-test") + collection "people" + docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]] + (monger.collection/remove db collection) + (is (monger.errors/ok? (monger.collection/insert-batch db "people" docs WriteConcern/NORMAL))) + (is (= 2 (monger.collection/count db collection))))) + + + + +;; +;; count +;; + (deftest get-collection-size (let [connection (monger.core/connect) db (monger.core/get-db connection "monger-test")]