diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 9189186..9471159 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -204,7 +204,7 @@ (check-not-nil! id "id must not be nil") (from-db-object ^DBObject (find-one-as-map collection { :_id id }) true)) ([^String collection id fields] - (check-not-nil! id "id must not be nil") + (check-not-nil! id "id must not be nil") (from-db-object ^DBObject (find-one-as-map collection { :_id id } fields) true)) ([^String collection id fields keywordize] (check-not-nil! id "id must not be nil") @@ -546,3 +546,14 @@ ([^DB db ^String collection ^String key ^Map query] (let [^DBCollection coll (.getCollection db collection)] (.distinct coll ^String (to-db-object key) ^DBObject (to-db-object query))))) + + +;; +;; create/capped collections +;; + +(defn create + "Creates a collection. Options are: :capped (pass true to create a capped collection), :max (number of documents) + and :size (max allowed size of the collection, in bytes)." + [^String name options] + (.createCollection ^DB monger.core/*mongodb-database* name (to-db-object options))) diff --git a/test/monger/test/capped_collections_test.clj b/test/monger/test/capped_collections_test.clj new file mode 100644 index 0000000..9c08b73 --- /dev/null +++ b/test/monger/test/capped_collections_test.clj @@ -0,0 +1,38 @@ +(set! *warn-on-reflection* true) + +(ns monger.test.capped-collections-test + (:require [monger core util] + [monger.collection :as mc] + [monger.result :as mres] + [monger.test.helper :as helper]) + (:use clojure.test + monger.operators + monger.test.fixtures)) + + +(helper/connect!) + +(use-fixtures :each purge-cached) + +(defn- megabytes + [^long n] + (* n 1024 1024)) + + +;; +;; Tests +;; + +(deftest test-inserting-into-capped-collection + (let [n 1000 + cname "cached" + _ (mc/drop cname) + coll (mc/create cname {:capped true :size (-> 16 megabytes) :max n})] + (is (= cname (.getName coll))) + (mc/insert-batch cname (for [i (range 0 (+ n 100))] {:i i})) + (is (= n (mc/count cname))) + ;; older elements get replaced by newer ones + (is (not (mc/any? cname {:i 1}))) + (is (not (mc/any? cname {:i 5}))) + (is (not (mc/any? cname {:i 9}))) + (is (mc/any? cname {:i (+ n 80)})))) diff --git a/test/monger/test/fixtures.clj b/test/monger/test/fixtures.clj index 8576f4e..bf6dfe5 100644 --- a/test/monger/test/fixtures.clj +++ b/test/monger/test/fixtures.clj @@ -15,3 +15,5 @@ (defcleaner domains "domains") (defcleaner pages "pages") +(defcleaner cached "cached") +