mongoDB-0.3: A driver for MongoDB





data Connection Source

A list of handles to database connections

data ConnectOpt Source



It's fine to connect to the slave

connect :: HostName -> [ConnectOpt] -> IO ConnectionSource

Establish a connection to a MongoDB server

connectOnPort :: HostName -> PortID -> [ConnectOpt] -> IO ConnectionSource

Establish a connection to a MongoDB server on a non-standard port

conClose :: Connection -> IO ()Source

Close database connection

dropDatabase :: Connection -> Database -> IO ()Source

Drop a database.

connectCluster :: [HostName] -> [ConnectOpt] -> IO ConnectionSource

Establish connections to a list of MongoDB servers

connectClusterOnPort :: [(HostName, PortID)] -> [ConnectOpt] -> IO ConnectionSource

Establish connections to a list of MongoDB servers specifying each port.

serverInfo :: Connection -> IO BsonDocSource

Get information about the MongoDB server we're connected to.

serverShutdown :: Connection -> IO BsonDocSource

Shut down the MongoDB server.

Force a clean exit, flushing and closing all data files. Note that it will wait until all ongoing operations are complete.

databasesInfo :: Connection -> IO BsonDocSource

Information about the databases on the server.

databaseNames :: Connection -> IO [Database]Source

Return a list of database names on the server.


type Database = ByteStringSource

The name of a database.

data ColCreateOpt Source


CCOSize Int64

Desired initial size for the collection (in bytes). must be less than or equal to 10000000000. For capped collections this size is the max size of the collection.

CCOCapped Bool

If True, this is a capped collection.

CCOMax Int64

Maximum number of objects if capped.

collectionNames :: Connection -> Database -> IO [FullCollection]Source

Return a list of collections in Database.

createCollection :: Connection -> FullCollection -> [ColCreateOpt] -> IO ()Source

Create a new collection in this database.

Normally collection creation is automatic. This function should only be needed if you want to specify ColCreateOpts on creation. MongoDBCollectionInvalid is thrown if the collection already exists.

dropCollection :: Connection -> FullCollection -> IO ()Source

Drop a collection.

renameCollection :: Connection -> FullCollection -> FullCollection -> IO ()Source

Rename a collection--first FullCollection argument is the existing name, the second is the new name. At the moment this command can also be used to move a collection between databases.

runCommand :: Connection -> Database -> BsonDoc -> IO BsonDocSource

Run a database command. Usually this is unneeded as driver wraps all of the commands for you (eg createCollection, dropCollection, etc).

validateCollection :: Connection -> FullCollection -> IO StringSource

Return a string of validation info about the collection.

Example output (note this probably can/will change with different versions of the server):

  details: 0x7fe5cc2c1da4 ofs:e7da4
  # extents:1
  datasize?:180 nrecords?:5 lastExtentSize:1024
  first extent:
    loc:0:24100 xnext:null xprev:null
    size:1024 firstRecord:0:241e4 lastRecord:0:24280
  5 objects found, nobj:5
  260 bytes data w/headers
  180 bytes data wout/headers
  deletedList: 0100100000000000000
  deleted: n: 4 size: 588
  nIndexes:1$_id_ keys:5

addUser :: Connection -> Database -> Username -> Password -> IO BsonDocSource

create a new user in the current Database

login :: Connection -> Database -> Username -> Password -> IO BsonDocSource

log into the mongodb Database attached to the Connection


type Collection = ByteStringSource

The same as FullCollection but without the Database prefix.

type FieldSelector = [ByteString]Source

A list of field names that limits the fields in the returned documents. The list can contains zero or more elements, each of which is the name of a field that should be returned. An empty list means that no limiting is done and all fields are returned.

type FullCollection = ByteStringSource

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 and the collection bar, the full collection name is

type NumToSkip = Int32Source

Sets the number of documents to omit - starting from the first document in the resulting dataset - when returning the result of the query.

type NumToReturn = Int32Source

This controls how many documents are returned at a time. The cursor works by requesting NumToReturn documents, which are then immediately all transfered over the network; these are held locally until the those NumToReturn are all consumed and then the network will be hit again for the next NumToReturn documents.

If the value 0 is given, the database will choose the number of documents to return.

Otherwise choosing a good value is very dependant on the document size and the way the cursor is being used.

type Selector = BsonDocSource

A BsonDoc representing restrictions for a query much like the where part of an SQL query.

data QueryOpt Source

Options that control the behavior of a query operation.


data UpdateFlag Source

Options that effect the behavior of a update operation.



count :: Connection -> FullCollection -> IO Int64Source

Return the number of documents in FullCollection.

countMatching :: Connection -> FullCollection -> Selector -> IO Int64Source

Return the number of documents in FullCollection matching Selector

delete :: Connection -> FullCollection -> Selector -> IO RequestIDSource

Delete documents matching Selector from the given FullCollection.

insert :: Connection -> FullCollection -> BsonDoc -> IO RequestIDSource

Insert a single document into FullCollection.

insertMany :: Connection -> FullCollection -> [BsonDoc] -> IO RequestIDSource

Insert a list of documents into FullCollection.

query :: Connection -> FullCollection -> [QueryOpt] -> NumToSkip -> NumToReturn -> Selector -> FieldSelector -> IO CursorSource

Open a cursor to find documents in FullCollection that match Selector. See the documentation for each argument's type for information about how it effects the query.

remove :: Connection -> FullCollection -> Selector -> IO RequestIDSource

An alias for delete.

update :: Connection -> FullCollection -> [UpdateFlag] -> Selector -> BsonDoc -> IO RequestIDSource

Update documents with BsonDoc in FullCollection that match Selector.

save :: Connection -> FullCollection -> BsonDoc -> IO RequestIDSource

Conveniently stores the BsonDoc to the FullCollection if there is an _id present in the BsonDoc then it already has a place in the DB, so we update it using the _id, otherwise we insert it

Convenience collection operations

find :: Connection -> FullCollection -> Selector -> IO CursorSource

Open a cursor to find documents. If you need full functionality, see query

findOne :: Connection -> FullCollection -> Selector -> IO (Maybe BsonDoc)Source

Query, but only return the first result, if any.

quickFind :: Connection -> FullCollection -> Selector -> IO [BsonDoc]Source

Perform a query and return the result as a lazy list. Be sure to understand the comments about using the lazy list given for allDocs.

quickFind' :: Connection -> FullCollection -> Selector -> IO [BsonDoc]Source

Perform a query and return the result as a strict list.

Query Helpers

whereClause :: String -> BsonDoc -> BsonDocSource

Use this in the place of the query portion of a select type query This uses javascript and a scope supplied by a BsonDoc to evaluate documents in the database for retrieval.


 findOne conn mycoll $ whereClause " == (name1 + name2)"
     (toBsonDoc [("name1", toBson "mar"), ("name2", toBson "tha")])


data Cursor Source

An Iterator 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.

allDocs :: Cursor -> IO [BsonDoc]Source

Return a lazy list of all (of the rest) of the documents in the cursor. This works much like hGetContents--it will lazily read the cursor data out of the database as the list is used. The cursor is automatically closed when the list has been fully read.

If you manually finish the cursor before consuming off this list you won't get all the original documents in the cursor.

If you don't consume to the end of the list, you must manually close the cursor or you will leak the cursor, which may also leak on the database side.

allDocs' :: Cursor -> IO [BsonDoc]Source

Returns a strict list of all (of the rest) of the documents in the cursor. This means that all of the documents will immediately be read out of the database and loaded into memory.

finish :: Cursor -> IO ()Source

Manually close a cursor -- usually not needed if you use allDocs, allDocs', or nextDoc.

nextDoc :: Cursor -> IO (Maybe BsonDoc)Source

Return one document or Nothing if there are no more. Automatically closes the cursor when last document is read


type Key = ByteStringSource

The field key to index on.

type Unique = BoolSource

Should this index guarantee uniqueness?

data Direction Source

Direction to index.



createIndex :: Connection -> FullCollection -> [(Key, Direction)] -> Unique -> IO ByteStringSource

Create a new index on FullCollection on the list of Key / Direction pairs.

dropIndex :: Connection -> FullCollection -> [(Key, Direction)] -> IO ()Source

Drop the specified index on the given FullCollection.

dropIndexes :: Connection -> FullCollection -> IO ()Source

Drop all indexes on FullCollection.

indexInformation :: Connection -> FullCollection -> IO [BsonDoc]Source

Return a BsonDoc describing the existing indexes on FullCollection.

With the current server versions (1.2) this will return documents such as:

 {"key": {"lastname": -1, "firstname": 1},
  "name": "lastname_-1_firstname_1",
  "ns": "mydb.people",
  "unique": true}

Which is a single key that indexes on lastname (descending) and then firstname (ascending) on the collection people of the database mydb with a uniqueness requirement.