collectionNames

This commit is contained in:
Scott R. Parish 2010-01-18 15:03:13 -06:00
parent cef52ebf12
commit 378bc42393
2 changed files with 17 additions and 3 deletions

View file

@ -28,7 +28,10 @@ module Database.MongoDB
-- * Connection
Connection,
connect, connectOnPort, conClose, disconnect,
-- * Basic database operations
-- * Database operations
Database,
collectionNames,
-- * Collection operations
Collection, FieldSelector, NumToSkip, NumToReturn, Selector,
QueryOpt(..),
UpdateFlag(..),
@ -52,8 +55,9 @@ import qualified Data.ByteString.Lazy.UTF8 as L8
import Data.Int
import Data.IORef
import qualified Data.List as List
import Data.Maybe
import Data.Typeable
import Database.MongoDB.BSON
import Database.MongoDB.BSON as BSON
import Database.MongoDB.Util
import qualified Network
import Network.Socket hiding (connect, send, sendTo, recv, recvFrom)
@ -88,6 +92,14 @@ conClose = hClose . cHandle
disconnect :: Connection -> IO ()
disconnect = conClose
-- | Return a list of collections in /Database/.
collectionNames :: Connection -> Database -> IO [Collection]
collectionNames c db = do
docs <- quickFind' c (db ++ ".system.namespaces") $ toBsonDoc []
let names = flip List.map docs $ \doc ->
fromBson $ fromJust $ BSON.lookup (L8.fromString "name") doc
return $ List.filter (not . List.elem '$') names
-- | An Itertaor over the results of a query. Use 'nextDoc' to get each
-- successive result document, or 'allDocs' or 'allDocs'' to get lazy or
-- strict lists of results.
@ -146,6 +158,9 @@ toOpcode 2006 = OP_DELETE
toOpcode 2007 = OP_KILL_CURSORS
toOpcode n = throw $ MongoDBInternalError $ "Got unexpected Opcode: " ++ show n
-- | The name of a database.
type Database = String
-- | The full collection name. The full collection name is the
-- concatenation of the database name with the collection name, using
-- a @.@ for the concatenation. For example, for the database @foo@

1
TODO
View file

@ -23,7 +23,6 @@ MongoDB
- pair mode connection
- operations on database objects
* getName
* getCollectionNames
* getCollection
* dropCollection
* executeCommand