dropCollection

This commit is contained in:
Scott R. Parish 2010-01-18 21:26:34 -06:00
parent 73d9a00d5f
commit 311e0462ac
2 changed files with 16 additions and 4 deletions

View file

@ -31,7 +31,7 @@ module Database.MongoDB
-- * Database -- * Database
Database, MongoDBCollectionInvalid, Database, MongoDBCollectionInvalid,
ColCreateOpt(..), ColCreateOpt(..),
collectionNames, createCollection, collectionNames, createCollection, dropCollection,
-- * Collection -- * Collection
Collection, FieldSelector, NumToSkip, NumToReturn, Selector, Collection, FieldSelector, NumToSkip, NumToReturn, Selector,
QueryOpt(..), QueryOpt(..),
@ -124,8 +124,8 @@ colCreateOptToBson (CCOMax m) = ("max", toBson m)
-- exists. -- exists.
createCollection :: Connection -> Collection -> [ColCreateOpt] -> IO () createCollection :: Connection -> Collection -> [ColCreateOpt] -> IO ()
createCollection c col opts = do createCollection c col opts = do
let db = List.takeWhile (/= '.') col let db = dbFromCol col
let col' = List.tail $ List.dropWhile (/= '.') col col' = colMinusDB col
dbcols <- collectionNames c db dbcols <- collectionNames c db
case col `List.elem` dbcols of case col `List.elem` dbcols of
True -> throwColInvalid $ "Collection already exists: " ++ show col True -> throwColInvalid $ "Collection already exists: " ++ show col
@ -146,6 +146,19 @@ createCollection c col opts = do
_ <- dbCmd c db $ toBsonDoc cmd _ <- dbCmd c db $ toBsonDoc cmd
return () return ()
dropCollection :: Connection -> Collection -> IO ()
dropCollection c col = do
let db = dbFromCol col
col' = colMinusDB col
_ <- dbCmd c db $ toBsonDoc [("drop", toBson col')]
return ()
dbFromCol :: Collection -> Database
dbFromCol = List.takeWhile (/= '.')
colMinusDB :: Collection -> Collection
colMinusDB = List.tail . List.dropWhile (/= '.')
dbCmd :: Connection -> Database -> BsonDoc -> IO BsonDoc dbCmd :: Connection -> Database -> BsonDoc -> IO BsonDoc
dbCmd c db cmd = do dbCmd c db cmd = do
mres <- findOne c (db ++ ".$cmd") cmd mres <- findOne c (db ++ ".$cmd") cmd

1
TODO
View file

@ -24,7 +24,6 @@ MongoDB
- operations on database objects - operations on database objects
* getName * getName
* getCollection * getCollection
* dropCollection
* executeCommand * executeCommand
* dropDatabase (erase) * dropDatabase (erase)
- database admin - database admin