Make it possible to pass fields to index as any collection (not just map)

This commit is contained in:
Michael S. Klishin 2012-06-11 00:06:13 +04:00
parent 52c5e15008
commit 4df673d127
3 changed files with 23 additions and 19 deletions

View file

@ -1,6 +1,9 @@
## Changes between 1.0.0-rc1 and 1.0.0-rc2 ## Changes between 1.0.0-rc1 and 1.0.0-rc2
No changes yet. ### More flexible monger.collection/ensure-index and monger.collection/create-index
`monger.collection/ensure-index` and `monger.collection/ensure-index` now accept fields to index as a collection
(e.g. a vector) as well as a map. It is convenient when creating multiple single-field indexes at once.

View file

@ -437,14 +437,14 @@
" "
([^String collection ^Map keys] ([^String collection ^Map keys]
(.createIndex (.getCollection monger.core/*mongodb-database* collection) (to-db-object keys))) (.createIndex (.getCollection monger.core/*mongodb-database* collection) (as-field-selector keys)))
([^String collection ^Map keys options] ([^String collection ^Map keys options]
(.createIndex (.getCollection monger.core/*mongodb-database* collection) (.createIndex (.getCollection monger.core/*mongodb-database* collection)
(to-db-object keys) (as-field-selector keys)
(to-db-object options))) (to-db-object options)))
([^DB db ^String collection ^Map keys ^Map options] ([^DB db ^String collection ^Map keys ^Map options]
(.createIndex (.getCollection db collection) (.createIndex (.getCollection db collection)
(to-db-object keys) (as-field-selector keys)
(to-db-object options)))) (to-db-object options))))
@ -464,14 +464,14 @@
(monger.collection/ensure-index \"pages\" {:url 1} {:unique true}) (monger.collection/ensure-index \"pages\" {:url 1} {:unique true})
" "
([^String collection ^Map keys] ([^String collection ^Map keys]
(.ensureIndex (.getCollection monger.core/*mongodb-database* collection) (to-db-object keys))) (.ensureIndex (.getCollection monger.core/*mongodb-database* collection) (as-field-selector keys)))
([^String collection ^Map keys ^Map options] ([^String collection ^Map keys ^Map options]
(.ensureIndex (.getCollection monger.core/*mongodb-database* collection) (.ensureIndex (.getCollection monger.core/*mongodb-database* collection)
(to-db-object keys) (as-field-selector keys)
(to-db-object options))) (to-db-object options)))
([^String collection ^Map keys ^String name ^Boolean unique?] ([^String collection ^Map keys ^String name ^Boolean unique?]
(.ensureIndex (.getCollection monger.core/*mongodb-database* collection) (.ensureIndex (.getCollection monger.core/*mongodb-database* collection)
(to-db-object keys) (as-field-selector keys)
name name
unique?))) unique?)))

View file

@ -2,8 +2,7 @@
(:import org.bson.types.ObjectId (:import org.bson.types.ObjectId
java.util.Date) java.util.Date)
(:require [monger core util] (:require [monger core util]
[monger.collection :as mgcol] [monger.collection :as mc]
[monger.result :as mgres]
[monger.test.helper :as helper]) [monger.test.helper :as helper])
(:use clojure.test (:use clojure.test
[monger operators conversion] [monger operators conversion]
@ -18,15 +17,17 @@
(deftest ^{:indexing true} test-creating-and-dropping-indexes (deftest ^{:indexing true} test-creating-and-dropping-indexes
(let [collection "libraries"] (let [collection "libraries"]
(mgcol/drop-indexes collection) (mc/drop-indexes collection)
(mgcol/create-index collection { "language" 1 }) (mc/create-index collection { "language" 1 })
(is (= "language_1" (is (= "language_1"
(:name (second (mgcol/indexes-on collection))))) (:name (second (mc/indexes-on collection)))))
(mgcol/drop-index collection "language_1") (mc/drop-index collection "language_1")
(is (nil? (second (mgcol/indexes-on collection)))) (mc/create-index collection ["language"])
(mgcol/ensure-index collection { "language" 1 } {:unique true}) (mc/drop-index collection "language_1")
(is (nil? (second (mc/indexes-on collection))))
(mc/ensure-index collection { "language" 1 } {:unique true})
(is (= "language_1" (is (= "language_1"
(:name (second (mgcol/indexes-on collection))))) (:name (second (mc/indexes-on collection)))))
(mgcol/ensure-index collection { "language" 1 }) (mc/ensure-index collection { "language" 1 })
(mgcol/ensure-index collection { "language" 1 } { :unique true }) (mc/ensure-index collection { "language" 1 } { :unique true })
(mgcol/drop-indexes collection))) (mc/drop-indexes collection)))