diff --git a/src/monger/collection.clj b/src/monger/collection.clj index b42d88f..d3e2ddd 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -157,10 +157,54 @@ (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] (.remove coll (to-db-object conditions))))) -;; monger.collection/ensure-index -;; monger.collection/drop-index +;; +;; monger.collection/create-index +;; + +(defn create-index + [^String collection, ^Map keys] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.createIndex coll (to-db-object keys)))) + + +;; +;; monger.collection/ensure-index +;; + +(defn ensure-index + ([^String collection, ^Map keys] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.ensureIndex coll (to-db-object keys)))) + ([^String collection, ^Map keys, ^String name] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.ensureIndex coll (to-db-object keys) name)))) + + +;; +;; monger.collection/indexes-on +;; + +(defn indexes-on + [^String collection] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (from-db-object (.getIndexInfo coll) true))) + + +;; +;; monger.collection/drop-index +;; + +(defn drop-index + [^String collection, ^String name] + (let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)] + (.dropIndex coll name))) + +(defn drop-indexes + [^String collection] + (.dropIndexes ^DBCollection (.getCollection monger.core/*mongodb-database* collection))) + ;; ;; Implementation diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index c48de95..cdbb762 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -1,8 +1,10 @@ (set! *warn-on-reflection* true) (ns monger.test.collection - (:import [com.mongodb WriteResult WriteConcern DBCursor DBObject] [java.util Date]) - (:require [monger core collection result util convertion] [clojure stacktrace]) + (:import [com.mongodb WriteResult WriteConcern DBCursor DBObject CommandResult$CommandFailure] + [java.util Date]) + (:require [monger core collection result util convertion] + [clojure stacktrace]) (:use [clojure.test])) (monger.util/with-ns 'monger.core @@ -239,7 +241,7 @@ ;; -;; monger.collection/find +;; find ;; (deftest find-multiple-documents-when-collection-is-empty @@ -358,3 +360,25 @@ (is (= 1 (monger.collection/count collection))) (is (= (modified-doc (monger.collection/find-by-id collection doc-id)))) (monger.collection/remove collection))) + + +;; +;; indexes +;; + +(deftest index-operations + (let [collection "libraries"] + (monger.collection/drop-indexes collection) + (is (= "_id_" + (:name (first (monger.collection/indexes-on collection))))) + (is (nil? (second (monger.collection/indexes-on collection)))) + (monger.collection/create-index collection { "language" 1 }) + (is (= "language_1" + (:name (second (monger.collection/indexes-on collection))))) + (monger.collection/drop-index collection "language_1") + (is (nil? (second (monger.collection/indexes-on collection)))) + (monger.collection/ensure-index collection { "language" 1 }) + (is (= "language_1" + (:name (second (monger.collection/indexes-on collection))))) + (monger.collection/ensure-index collection { "language" 1 }))) +