-- ^ Run db operation with exclusive access to the connection
runDbOpdbOp=ReaderT(runOp.flipuseDbdbOp)
-- * Authentication
auth::(Connm)=>Username->Password->DbmBool
-- ^ Authenticate with the database (if server is running in secure mode). Return whether authentication was successful or not. Reauthentication is required for every new connection.
-- ^ Selects documents in collection that match selector
select::Selector->Collection->Selection
-- ^ Synonym for 'Select'
select=Select
typeSelector=Document
-- ^ Filter for a query, analogous to the where clause in SQL. @[]@ matches all documents in collection. @[x =: a, y =: b]@ is analogous to @where x = a and y = b@ in SQL. See <http://www.mongodb.org/display/DOCS/Querying> for full selector syntax.
whereJS::Selector->Javascript->Selector
-- ^ Add Javascript predicate to selector, in which case a document must match both selector and predicate
whereJSseljs=("$where"=:js):sel
-- * Write
-- ** Insert
insert::(Connm)=>Collection->Document->DbmValue
-- ^ Insert document into collection and return its \"_id\" value, which is created automatically if not supplied
-- ^ Update first document in selection using updater document, unless 'MultiUpdate' option is supplied then update all documents in selection. If 'Upsert' option is supplied then treat updater as document and insert it if selection is empty.
skip::Word32,-- ^ Number of initial matching documents to skip
limit::Limit,-- ^ Maximum number of documents to return, 0 = no limit
sort::Order,-- ^ Sort results by this order, [] = no sort
snapshot::Bool,-- ^ If true assures no duplicates are returned, or objects missed, which were present at both the start and end of the query's execution (even if the object were updated). If an object is new during the query, or deleted during the query, it may or may not be returned, even with snapshot mode. Note that short query responses (less than 1MB) are always effectively snapshotted.
batchSize::BatchSize,-- ^ The number of document to return in each batch response from the server. 0 means use Mongo default.
hint::Order-- ^ Force MongoDB to use this index, [] = no hint
}deriving(Show,Eq)
typeProjector=Document
-- ^ Fields to return, analogous to the select clause in SQL. @[]@ means return whole document (analogous to * in SQL). @[x =: 1, y =: 1]@ means return only @x@ and @y@ fields of each document. @[x =: 0]@ means return all fields except @x@.
typeLimit=Word32
-- ^ Maximum number of documents to return, i.e. cursor will close after iterating over this number of documents. 0 means no limit.
typeOrder=Document
-- ^ Fields to sort by. Each one is associated with 1 or -1. Eg. @[x =: 1, y =: (-1)]@ means sort by @x@ ascending then @y@ descending
typeBatchSize=Word32
-- ^ The number of document to return in each batch response from the server. 0 means use Mongo default.
query::Selector->Collection->Query
-- ^ Selects documents in collection that match selector. It uses no query options, projects all fields, does not skip any documents, does not limit result size, uses default batch size, does not sort, does not hint, and does not snapshot.
-- ^ Iterator over results of a query. Use 'next' to iterate or 'rest' to get all results. A cursor is closed when it is explicitly closed, all results have been read from it, garbage collected, or not used for over 10 minutes (unless 'NoCursorTimeout' option was specified in 'Query'). Reading from a closed cursor raises a ServerFailure exception. Note, a cursor is not closed when the connection is closed, so you can open another connection to the same server and continue using the cursor.
-- ^ Return next document in query result, or Nothing if finished.
-- This can run inside or outside a 'Db' monad (a 'useDb' block), since @Conn m => ReaderT r m@ is an instance of the 'Conn' type class, along with @Task@ and @Op@
-- Get lock on connection (runOp) first then get lock on cursor, otherwise you could get in deadlock if already inside an Op (connection locked), but another Task gets lock on cursor first and then tries runOp (deadlock).
gReduce::Javascript,-- ^ The reduce function aggregates (reduces) the objects iterated. Typical operations of a reduce function include summing and counting. reduce takes two arguments: the current document being iterated over and the aggregation value.
gInitial::Document,-- ^ Initial aggregation value supplied to reduce
gCond::Selector,-- ^ Condition that must be true for a row to be considered. [] means always true.
gFinalize::MaybeJavascript-- ^ An optional function to be run on each item in the result set just before the item is returned. Can either modify the item (e.g., add an average field given a count and a total) or return a replacement object (returning a new object with just _id and average fields).
-- ^ Fields to group by, or function returning a "key object" to be used as the grouping key. Use this instead of key to specify a key that is not an existing member of the object (or, to access embedded members).
groupDocument::Group->Document
-- ^ Translate Group data into expected document form
-- | Maps every document in collection to a (key, value) pair, then for each unique key reduces all its associated values to a result. Therefore, the final output is a list of (key, result) pairs, where every key is unique. This is the basic description. There are additional nuances that may be used. See <http://www.mongodb.org/display/DOCS/MapReduce> for details.
rOut::MaybeCollection,-- ^ Output to given permanent collection, otherwise output to a new temporary collection whose name is returned.
rKeepTemp::Bool,-- ^ If True, the temporary output collection is made permanent. If False, the temporary output collection persists for the life of the current connection only, however, other connections may read from it while the original one is still alive. Note, reading from a temporary collection after its original connection dies returns an empty result (not an error). The default for this attribute is False, unless 'rOut' is specified, then the collection permanent.
rFinalize::MaybeFinalizeFun,-- ^ Function to apply to all the results when finished. Default is Nothing.
rScope::Document,-- ^ Variables (environment) that can be accessed from map/reduce/finalize. Default is [].
rVerbose::Bool-- ^ Provide statistics on job execution time. Default is False.
}deriving(Show,Eq)
typeMapFun=Javascript
-- ^ @() -> void@. The map function references the variable this to inspect the current object under consideration. A map function must call @emit(key,value)@ at least once, but may be invoked any number of times, as may be appropriate.
typeReduceFun=Javascript
-- ^ @(key, value_array) -> value@. The reduce function receives a key and an array of values. To use, reduce the received values, and return a result. The MapReduce engine may invoke reduce functions iteratively; thus, these functions must be idempotent. That is, the following must hold for your reduce function: @for all k, vals : reduce(k, [reduce(k,vals)]) == reduce(k,vals)@. If you need to perform an operation only once, use a finalize function. The output of emit (the 2nd param) and reduce should be the same format to make iterative reduce possible.
typeFinalizeFun=Javascript
-- ^ @(key, value) -> final_value@. A finalize function may be run after reduction. Such a function is optional and is not necessary for many map/reduce cases. The finalize function takes a key and a value, and returns a finalized value.
mrDocument::MapReduce->Document
-- ^ Translate MapReduce data into expected document form
-- ^ Run MapReduce and return cursor of results. Error if map/reduce fails (because of bad Javascript)
-- TODO: Delete temp result collection when cursor closes. Until then, it will be deleted by the server when connection closes.
runMRmr=find.query[]=<<(at"result"<$>runMR'mr)
runMR'::(Connm)=>MapReduce->DbmDocument
-- ^ Run MapReduce and return a result document containing a "result" field holding the output Collection and additional statistic fields. Error if the map/reduce failed (because of bad Javascript).
-- ^ Fetch what the last error was, Nothing means no error. Especially useful after a write since it is asynchronous (ie. nothing is returned after a write, so we don't know if it succeeded or not). To ensure no interleaving db operation executes between the write we want to check and getLastError, this can only be executed inside a 'runDbOp' which gets exclusive access to the connection.