mongoDB-0.1: A driver for MongoDB

Database.MongoDB

Contents

Synopsis

Connection

data Connection Source

A handle to a database connection

connect :: HostName -> IO ConnectionSource

Estabilish a connection to a MongoDB server

connectOnPort :: HostName -> PortID -> IO ConnectionSource

Estabilish 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.

Database

type Database = StringSource

The name of a database.

data ColCreateOpt Source

Constructors

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 [Collection]Source

Return a list of collections in Database.

createCollection :: Connection -> Collection -> [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 -> Collection -> IO ()Source

Drop a collection.

validateCollection :: Connection -> Collection -> 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):

 validate
  details: 0x7fe5cc2c1da4 ofs:e7da4
  firstExtent:0:24100 ns:test.foo.bar
  lastExtent:0:24100 ns:test.foo.bar
  # extents:1
  datasize?:180 nrecords?:5 lastExtentSize:1024
  padding:1
  first extent:
    loc:0:24100 xnext:null xprev:null
    nsdiag:test.foo.bar
    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
    test.foo.bar.$_id_ keys:5

Collection

type Collection = StringSource

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 foo.bar.

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

Instances

data UpdateFlag Source

Options that effect the behavior of a update operation.

Constructors

UF_Upsert 
UF_Multiupdate 

count :: Connection -> Collection -> IO Int64Source

Return the number of documents in Collection.

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

Return the number of documents in Collection matching Selector

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

Delete documents matching Selector from the given Collection.

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

Insert a single document into Collection.

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

Insert a list of documents into Collection.

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

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

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

An alias for delete.

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

Update documents with BsonDoc in Collection that match Selector.

Convience collection operations

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

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

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

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

quickFind :: Connection -> Collection -> 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 -> Collection -> Selector -> IO [BsonDoc]Source

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

Cursor

data Cursor Source

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.

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 curosr when last document is read