Skip to content
Permalink
Browse files

delete-index! works with proper multi-key ordering, as well as named …

…indexes
  • Loading branch information
christophermaier committed Apr 16, 2011
1 parent d0be2f8 commit 65a38350549b8cfb121eb705be27135a39163577
Showing with 47 additions and 9 deletions.
  1. +12 −3 src/somnium/congomongo.clj
  2. +35 −6 test/somnium/test/congomongo.clj
@@ -305,9 +305,18 @@ releases. Please use 'make-connection' in combination with
[:clojure :mongo]))))

(defn drop-index!
"Drops an index on the collection for the specified fields"
[coll fields]
(.dropIndex (get-coll coll) (coerce-fields fields)))
"Drops an index on the collection for the specified fields.
`index` may be a vector representing the key(s) of the index (see somnium.congomongo/add-index! for the
expected format). It may also be a String or Keyword, in which case it is taken to be the name of the
index to be deleted.
Due to how the underlying MongoDB driver works, if you defined an index with a custom name, you *must*
delete the index using that name, and not the keys."
[coll index]
(if (vector? index)
(.dropIndex (get-coll coll) (coerce-index-fields index))
(.dropIndex (get-coll coll) (coerce index [:clojure :mongo]))))

(defn drop-all-indexes!
"Drops all indexes from a collection"
@@ -169,15 +169,24 @@
(is (some #(= (into {} (% "key")) {"x" 1})
(get-indexes :points)))))

(defn- get-named-index [coll name]
(first (filter #(= (get % "name") name)
(get-indexes coll))))
(defn- get-index
"Retrieve an index, either by name or by key vector"
[coll index]
(let [selector (if (vector? index)
(fn [i]
(= (get i "key")
(coerce-index-fields index)))
(fn [i]
(= (get i "name")
index)))]
(first (filter selector (get-indexes coll)))))


(deftest complex-indexing
(with-test-mongo
(add-index! :testing-indexes [:a :b :c])
(let [auto-generated-index-name "a_1_b_1_c_1"
actual-index (get (get-named-index :testing-indexes auto-generated-index-name)
actual-index (get (get-index :testing-indexes auto-generated-index-name)
"key")
expected-index (doto (BasicDBObject.)
(.put "a" 1)
@@ -187,7 +196,7 @@

(add-index! :testing-indexes [:a [:b -1] :c])
(let [auto-generated-index-name "a_1_b_-1_c_1"
actual-index (get (get-named-index :testing-indexes auto-generated-index-name)
actual-index (get (get-index :testing-indexes auto-generated-index-name)
"key")
expected-index (doto (BasicDBObject.)
(.put "a" 1)
@@ -200,9 +209,29 @@
(let [coll :test-index-name
index "customIndexName"]
(add-index! coll [:foo :bar :baz] :name index)
(is (= (get (get-named-index coll index)
(is (= (get (get-index coll index)
"key"))))))

(deftest test-delete-index
(with-test-mongo
(let [test-collection :testing-indexes
index-name "test_index"
index-key [:c :b [:a -1]]]
;; Test using keys
(is (nil? (get-index test-collection index-key)))
(add-index! test-collection index-key)
(is (get-index test-collection index-key))
(drop-index! test-collection index-key)
(is (nil? (get-index test-collection index-key)))

;; Test using names
(is (nil? (get-index test-collection index-name)))
(add-index! test-collection index-key :name index-name)
(is (get-index test-collection index-name))
(drop-index! test-collection index-name)
(is (nil? (get-index test-collection index-name))))))


(defrecord Foo [a b])

(deftest can-insert-records-as-maps

0 comments on commit 65a3835

Please sign in to comment.
You can’t perform that action at this time.