dropCollection
This commit is contained in:
parent
73d9a00d5f
commit
311e0462ac
2 changed files with 16 additions and 4 deletions
|
@ -31,7 +31,7 @@ module Database.MongoDB
|
|||
-- * Database
|
||||
Database, MongoDBCollectionInvalid,
|
||||
ColCreateOpt(..),
|
||||
collectionNames, createCollection,
|
||||
collectionNames, createCollection, dropCollection,
|
||||
-- * Collection
|
||||
Collection, FieldSelector, NumToSkip, NumToReturn, Selector,
|
||||
QueryOpt(..),
|
||||
|
@ -124,8 +124,8 @@ colCreateOptToBson (CCOMax m) = ("max", toBson m)
|
|||
-- exists.
|
||||
createCollection :: Connection -> Collection -> [ColCreateOpt] -> IO ()
|
||||
createCollection c col opts = do
|
||||
let db = List.takeWhile (/= '.') col
|
||||
let col' = List.tail $ List.dropWhile (/= '.') col
|
||||
let db = dbFromCol col
|
||||
col' = colMinusDB col
|
||||
dbcols <- collectionNames c db
|
||||
case col `List.elem` dbcols of
|
||||
True -> throwColInvalid $ "Collection already exists: " ++ show col
|
||||
|
@ -146,6 +146,19 @@ createCollection c col opts = do
|
|||
_ <- dbCmd c db $ toBsonDoc cmd
|
||||
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 c db cmd = do
|
||||
mres <- findOne c (db ++ ".$cmd") cmd
|
||||
|
|
1
TODO
1
TODO
|
@ -24,7 +24,6 @@ MongoDB
|
|||
- operations on database objects
|
||||
* getName
|
||||
* getCollection
|
||||
* dropCollection
|
||||
* executeCommand
|
||||
* dropDatabase (erase)
|
||||
- database admin
|
||||
|
|
Loading…
Reference in a new issue