Implement monger.collection/exists? and monger.collection/drop

This commit is contained in:
Michael S. Klishin 2011-09-10 08:09:32 +04:00
parent 1f9b611003
commit 24b2dbe516
2 changed files with 31 additions and 5 deletions

View file

@ -8,7 +8,7 @@
;; You must not remove this notice, or any other, from this software.
(ns monger.collection
(:refer-clojure :exclude [find remove count])
(:refer-clojure :exclude [find remove count drop])
(:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map) (clojure.lang IPersistentMap ISeq))
(:require [monger core result])
(:use [monger.convertion]))
@ -174,11 +174,11 @@
(defn ensure-index
([^String collection, ^Map keys]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.ensureIndex coll (to-db-object keys))))
(let [coll ^DBCollection (.getCollection monger.core/*mongodb-database* collection)]
(.ensureIndex ^DBCollection coll ^DBObject (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))))
(let [coll ^DBCollection (.getCollection monger.core/*mongodb-database* collection)]
(.ensureIndex coll ^DBObject (to-db-object keys) ^String name))))
;;
@ -205,6 +205,16 @@
(.dropIndexes ^DBCollection (.getCollection monger.core/*mongodb-database* collection)))
(defn exists?
[^String collection]
(.collectionExists monger.core/*mongodb-database* collection))
(defn drop
[^String collection]
(let [^DBCollection coll (.getCollection monger.core/*mongodb-database* collection)]
(.drop coll)))
;;
;; Implementation
;;

View file

@ -382,3 +382,19 @@
(:name (second (monger.collection/indexes-on collection)))))
(monger.collection/ensure-index collection { "language" 1 })))
;;
;; exists?, drop
;;
(deftest checking-for-collection-existence-when-it-does-not-exist
(let [collection "widgets"]
(monger.collection/drop collection)
(is (false? (monger.collection/exists? collection)))))
(deftest checking-for-collection-existence-when-it-does-exist
(let [collection "widgets"]
(monger.collection/drop collection)
(monger.collection/insert-batch collection [{ :name "widget1" }
{ :name "widget2" }])
(is (monger.collection/exists? collection))))