diff --git a/CHANGELOG.md b/CHANGELOG.md
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,80 @@
 All notable changes to this project will be documented in this file.
 This project adheres to [Package Versioning Policy](https://wiki.haskell.org/Package_versioning_policy).
 
+* Get rid of `MonadFail` constraints in `Database.MongoDB.Query`
+
+## [2.7.1.4] - 2024-02-18
+
+### Fixed
+- Lower bound of base package
+
+## [2.7.1.3] - 2024-02-04
+
+### Fixed
+- Collections with dot in the name
+- Upper limit for batch size in Query.splitAtLimit
+
+## [2.7.1.2] - 2022-10-26
+
+### Added
+- Support of OP_MSG protocol
+- Clarifications in the documentation
+- Allow optional TLS parameters
+
+## [2.7.1.1] - 2021-06-14
+
+### Fixed
+- Sample code
+
+### Added
+- Clarification to the tutorial
+
+## [2.7.1.0] - 2020-08-17
+
+### Added
+- Function findCommand
+
+## [2.7.0.1] - 2020-04-07
+
+### Fixed
+- Error reporting for deletion of big messages
+- Formatting of docs
+
+## [2.7.0.0] - 2020-02-08
+
+### Fixed
+- Upgraded bson to compile with GHC 8.8
+
+## [2.6.0.1] - 2020-02-01
+
+### Fixed
+- Parsing hostname with underscores in readHostPortM.
+
+## [2.6.0.0] - 2020-01-03
+
+### Added
+- MonadFail. It's a standard for newer versions of Haskell,
+- Open replica sets over tls.
+
+### Fixed
+- Support for unix domain socket connection,
+- Stubborn listener threads.
+
+## [2.5.0.0] - 2019-06-14
+
+### Fixed
+Compatibility with network 3.0 package
+
+## [2.4.0.1] - 2019-03-03
+
+### Fixed
+Doc for modify method
+
+## [2.4.0.0] - 2018-05-03
+
+### Fixed
+- GHC 8.4 compatibility. isEmptyChan is not available in base 4.11 anymore.
+
 ## [2.3.0.5] - 2018-03-15
 
 ### Fixed
diff --git a/Database/MongoDB/Admin.hs b/Database/MongoDB/Admin.hs
--- a/Database/MongoDB/Admin.hs
+++ b/Database/MongoDB/Admin.hs
@@ -76,8 +76,8 @@
     db <- thisDatabase
     useDb admin $ runCommand ["renameCollection" =: db <.> from, "to" =: db <.> to, "dropTarget" =: True]
 
-dropCollection :: (MonadIO m) => Collection -> Action m Bool
--- ^ Delete the given collection! Return True if collection existed (and was deleted); return False if collection did not exist (and no action).
+dropCollection :: (MonadIO m, MonadFail m) => Collection -> Action m Bool
+-- ^ Delete the given collection! Return @True@ if collection existed (and was deleted); return @False@ if collection did not exist (and no action).
 dropCollection coll = do
     resetIndexCache
     r <- runCommand ["drop" =: coll]
@@ -86,7 +86,7 @@
             fail $ "dropCollection failed: " ++ show r
 
 validateCollection :: (MonadIO m) => Collection -> Action m Document
--- ^ This operation takes a while
+-- ^ Validate the given collection, scanning the data and indexes for correctness. This operation takes a while.
 validateCollection coll = runCommand ["validate" =: coll]
 
 -- ** Index
@@ -111,7 +111,7 @@
     "dropDups" =: iDropDups ] ++ (maybeToList $ fmap ((=:) "expireAfterSeconds") iExpireAfterSeconds)
 
 index :: Collection -> Order -> Index
--- ^ Spec of index of ordered keys on collection. Name is generated from keys. Unique and dropDups are False.
+-- ^ Spec of index of ordered keys on collection. 'iName' is generated from keys. 'iUnique' and 'iDropDups' are @False@.
 index coll keys = Index coll keys (genName keys) False False Nothing
 
 genName :: Order -> IndexName
@@ -132,7 +132,7 @@
 createIndex idx = insert_ "system.indexes" . idxDocument idx =<< thisDatabase
 
 dropIndex :: (MonadIO m) => Collection -> IndexName -> Action m Document
--- ^ Remove the index
+-- ^ Remove the index from the given collection.
 dropIndex coll idxName = do
     resetIndexCache
     runCommand ["deleteIndexes" =: coll, "index" =: idxName]
@@ -197,7 +197,7 @@
 
 addUser :: (MonadIO m)
         => Bool -> Username -> Password -> Action m ()
--- ^ Add user with password with read-only access if bool is True or read-write access if bool is False
+-- ^ Add user with password with read-only access if the boolean argument is @True@, or read-write access if it's @False@
 addUser readOnly user pass = do
     mu <- findOne (select ["user" =: user] "system.users")
     let usr = merge ["readOnly" =: readOnly, "pwd" =: pwHash user pass] (maybe ["user" =: user] id mu)
@@ -210,11 +210,11 @@
 -- ** Database
 
 admin :: Database
--- ^ \"admin\" database
+-- ^ The \"admin\" database, which stores user authorization and authentication data plus other system collections.
 admin = "admin"
 
 cloneDatabase :: (MonadIO m) => Database -> Host -> Action m Document
--- ^ Copy database from given host to the server I am connected to. Fails and returns @"ok" = 0@ if we don't have permission to read from given server (use copyDatabase in this case).
+-- ^ Copy database from given host to the server I am connected to. Fails and returns @"ok" = 0@ if we don't have permission to read from given server (use 'copyDatabase' in this case).
 cloneDatabase db fromHost = useDb db $ runCommand ["clone" =: showHostPort fromHost]
 
 copyDatabase :: (MonadIO m) => Database -> Host -> Maybe (Username, Password) -> Database -> Action m Document
@@ -238,9 +238,11 @@
 -- ** Server
 
 serverBuildInfo :: (MonadIO m) => Action m Document
+-- ^ Return a document containing the parameters used to compile the server instance.
 serverBuildInfo = useDb admin $ runCommand ["buildinfo" =: (1 :: Int)]
 
 serverVersion :: (MonadIO m) => Action m Text
+-- ^ Return the version of the server instance.
 serverVersion = at "version" `liftM` serverBuildInfo
 
 -- * Diagnostics
@@ -248,15 +250,19 @@
 -- ** Collection
 
 collectionStats :: (MonadIO m) => Collection -> Action m Document
+-- ^ Return some storage statistics for the given collection.
 collectionStats coll = runCommand ["collstats" =: coll]
 
 dataSize :: (MonadIO m) => Collection -> Action m Int
+-- ^ Return the total uncompressed size (in bytes) in memory of all records in the given collection. Does not include indexes.
 dataSize c = at "size" `liftM` collectionStats c
 
 storageSize :: (MonadIO m) => Collection -> Action m Int
+-- ^ Return the total bytes allocated to the given collection. Does not include indexes.
 storageSize c = at "storageSize" `liftM` collectionStats c
 
 totalIndexSize :: (MonadIO m) => Collection -> Action m Int
+-- ^ The total size in bytes of all indexes in this collection.
 totalIndexSize c = at "totalIndexSize" `liftM` collectionStats c
 
 totalSize :: MonadIO m => Collection -> Action m Int
@@ -269,34 +275,45 @@
 
 -- ** Profiling
 
-data ProfilingLevel = Off | Slow | All  deriving (Show, Enum, Eq)
+-- | The available profiler levels.
+data ProfilingLevel
+    = Off -- ^ No data collection.
+    | Slow -- ^ Data collected only for slow operations. The slow operation time threshold is 100ms by default, but can be changed using 'setProfilingLevel'.
+    | All -- ^ Data collected for all operations.
+    deriving (Show, Enum, Eq)
 
 getProfilingLevel :: (MonadIO m) => Action m ProfilingLevel
+-- ^ Get the profiler level.
 getProfilingLevel = (toEnum . at "was") `liftM` runCommand ["profile" =: (-1 :: Int)]
 
 type MilliSec = Int
 
 setProfilingLevel :: (MonadIO m) => ProfilingLevel -> Maybe MilliSec -> Action m ()
+-- ^ Set the profiler level, and optionally the slow operation time threshold (in milliseconds).
 setProfilingLevel p mSlowMs =
     runCommand (["profile" =: fromEnum p] ++ ("slowms" =? mSlowMs)) >> return ()
 
 -- ** Database
 
 dbStats :: (MonadIO m) => Action m Document
+-- ^ Return some storage statistics for the given database.
 dbStats = runCommand ["dbstats" =: (1 :: Int)]
 
 currentOp :: (MonadIO m) => Action m (Maybe Document)
 -- ^ See currently running operation on the database, if any
 currentOp = findOne (select [] "$cmd.sys.inprog")
 
+-- | An operation indentifier.
 type OpNum = Int
 
 killOp :: (MonadIO m) => OpNum -> Action m (Maybe Document)
+-- ^ Terminate the operation specified by the given 'OpNum'.
 killOp op = findOne (select ["op" =: op] "$cmd.sys.killop")
 
 -- ** Server
 
 serverStatus :: (MonadIO m) => Action m Document
+-- ^ Return a document with an overview of the state of the database.
 serverStatus = useDb admin $ runCommand ["serverStatus" =: (1 :: Int)]
 
 
diff --git a/Database/MongoDB/Connection.hs b/Database/MongoDB/Connection.hs
--- a/Database/MongoDB/Connection.hs
+++ b/Database/MongoDB/Connection.hs
@@ -17,29 +17,29 @@
     Host(..), PortID(..), defaultPort, host, showHostPort, readHostPort,
     readHostPortM, globalConnectTimeout, connect, connect',
     -- * Replica Set
-    ReplicaSetName, openReplicaSet, openReplicaSet',
+    ReplicaSetName, openReplicaSet, openReplicaSet', openReplicaSetTLS, openReplicaSetTLS',
+    openReplicaSetSRV, openReplicaSetSRV', openReplicaSetSRV'', openReplicaSetSRV''',
     ReplicaSet, primary, secondaryOk, routedHost, closeReplicaSet, replSetName
 ) where
 
 import Prelude hiding (lookup)
 import Data.IORef (IORef, newIORef, readIORef)
 import Data.List (intersect, partition, (\\), delete)
+import Data.Maybe (fromJust)
 
 #if !MIN_VERSION_base(4,8,0)
 import Control.Applicative ((<$>))
 #endif
 
-import Control.Monad (forM_)
-import Network (HostName, PortID(..), connectTo)
+import Control.Monad (forM_, guard)
 import System.IO.Unsafe (unsafePerformIO)
 import System.Timeout (timeout)
-import Text.ParserCombinators.Parsec (parse, many1, letter, digit, char, eof,
+import Text.ParserCombinators.Parsec (parse, many1, letter, digit, char, anyChar, eof,
                                       spaces, try, (<|>))
 import qualified Data.List as List
 
 
-import Control.Monad.Identity (runIdentity)
-import Control.Monad.Error (throwError)
+import Control.Monad.Except (throwError)
 import Control.Concurrent.MVar.Lifted (MVar, newMVar, withMVar, modifyMVar,
                                        readMVar)
 import Data.Bson (Document, at, (=:))
@@ -48,11 +48,13 @@
 import qualified Data.Bson as B
 import qualified Data.Text as T
 
+import Database.MongoDB.Internal.Network (Host(..), HostName, PortID(..), connectTo, lookupSeedList, lookupReplicaSetName)
 import Database.MongoDB.Internal.Protocol (Pipe, newPipe, close, isClosed)
 import Database.MongoDB.Internal.Util (untilSuccess, liftIOE,
                                        updateAssocs, shuffle, mergesortM)
 import Database.MongoDB.Query (Command, Failure(ConnectionFailure), access,
                               slaveOk, runCommand, retrieveServerData)
+import qualified Database.MongoDB.Transport.Tls as TLS (connect)
 
 adminCommand :: Command -> Pipe -> IO Document
 -- ^ Run command against admin database on server connected to pipe. Fail if connection fails.
@@ -62,10 +64,6 @@
     failureToIOError (ConnectionFailure e) = e
     failureToIOError e = userError $ show e
 
--- * Host
-
-data Host = Host HostName PortID  deriving (Show, Eq, Ord)
-
 defaultPort :: PortID
 -- ^ Default MongoDB port = 27017
 defaultPort = PortNumber 27017
@@ -76,46 +74,50 @@
 
 showHostPort :: Host -> String
 -- ^ Display host as \"host:port\"
--- TODO: Distinguish Service and UnixSocket port
-showHostPort (Host hostname port) = hostname ++ ":" ++ portname  where
-    portname = case port of
-        Service s -> s
-        PortNumber p -> show p
+-- TODO: Distinguish Service port
+showHostPort (Host hostname (PortNumber port)) = hostname ++ ":" ++ show port
 #if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS) && !defined(_WIN32)
-        UnixSocket s -> s
+showHostPort (Host _        (UnixSocket path)) = "unix:" ++ path
 #endif
 
-readHostPortM :: (Monad m) => String -> m Host
+readHostPortM :: (MonadFail m) => String -> m Host
 -- ^ Read string \"hostname:port\" as @Host hosthame (PortNumber port)@ or \"hostname\" as @host hostname@ (default port). Fail if string does not match either syntax.
--- TODO: handle Service and UnixSocket port
+
+-- TODO: handle Service port
 readHostPortM = either (fail . show) return . parse parser "readHostPort" where
-    hostname = many1 (letter <|> digit <|> char '-' <|> char '.')
+    hostname = many1 (letter <|> digit <|> char '-' <|> char '.' <|> char '_')
     parser = do
         spaces
         h <- hostname
         try (spaces >> eof >> return (host h)) <|> do
             _ <- char ':'
-            port :: Int <- read <$> many1 digit
-            spaces >> eof
-            return $ Host h (PortNumber $ fromIntegral port)
+            try (  do port :: Int <- read <$> many1 digit
+                      spaces >> eof
+                      return $ Host h (PortNumber $ fromIntegral port))
+#if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS) && !defined(_WIN32)
+              <|>  do guard (h == "unix")
+                      p <- many1 anyChar
+                      eof
+                      return $ Host "" (UnixSocket p)
+#endif
 
 readHostPort :: String -> Host
 -- ^ Read string \"hostname:port\" as @Host hostname (PortNumber port)@ or \"hostname\" as @host hostname@ (default port). Error if string does not match either syntax.
-readHostPort = runIdentity . readHostPortM
+readHostPort = fromJust . readHostPortM
 
 type Secs = Double
 
 globalConnectTimeout :: IORef Secs
--- ^ 'connect' (and 'openReplicaSet') fails if it can't connect within this many seconds (default is 6 seconds). Use 'connect\'' (and 'openReplicaSet\'') if you want to ignore this global and specify your own timeout. Note, this timeout only applies to initial connection establishment, not when reading/writing to the connection.
+-- ^ 'connect' (and 'openReplicaSet') fails if it can't connect within this many seconds (default is 6 seconds). Use 'connect'' (and 'openReplicaSet'') if you want to ignore this global and specify your own timeout. Note, this timeout only applies to initial connection establishment, not when reading/writing to the connection.
 globalConnectTimeout = unsafePerformIO (newIORef 6)
 {-# NOINLINE globalConnectTimeout #-}
 
 connect :: Host -> IO Pipe
--- ^ Connect to Host returning pipelined TCP connection. Throw IOError if connection refused or no response within 'globalConnectTimeout'.
+-- ^ Connect to Host returning pipelined TCP connection. Throw 'IOError' if connection refused or no response within 'globalConnectTimeout'.
 connect h = readIORef globalConnectTimeout >>= flip connect' h
 
 connect' :: Secs -> Host -> IO Pipe
--- ^ Connect to Host returning pipelined TCP connection. Throw IOError if connection refused or no response within given number of seconds.
+-- ^ Connect to Host returning pipelined TCP connection. Throw 'IOError' if connection refused or no response within given number of seconds.
 connect' timeoutSecs (Host hostname port) = do
     mh <- timeout (round $ timeoutSecs * 1000000) (connectTo hostname port)
     handle <- maybe (ioError $ userError "connect timed out") return mh
@@ -128,32 +130,85 @@
 
 type ReplicaSetName = Text
 
+data TransportSecurity = Secure | Unsecure
+
 -- | Maintains a connection (created on demand) to each server in the named replica set
-data ReplicaSet = ReplicaSet ReplicaSetName (MVar [(Host, Maybe Pipe)]) Secs
+data ReplicaSet = ReplicaSet ReplicaSetName (MVar [(Host, Maybe Pipe)]) Secs TransportSecurity
 
 replSetName :: ReplicaSet -> Text
--- ^ name of connected replica set
-replSetName (ReplicaSet rsName _ _) = rsName
+-- ^ Get the name of connected replica set.
+replSetName (ReplicaSet rsName _ _ _) = rsName
 
 openReplicaSet :: (ReplicaSetName, [Host]) -> IO ReplicaSet
--- ^ Open connections (on demand) to servers in replica set. Supplied hosts is seed list. At least one of them must be a live member of the named replica set, otherwise fail. The value of 'globalConnectTimeout' at the time of this call is the timeout used for future member connect attempts. To use your own value call 'openReplicaSet\'' instead.
+-- ^ Open connections (on demand) to servers in replica set. Supplied hosts is seed list. At least one of them must be a live member of the named replica set, otherwise fail. The value of 'globalConnectTimeout' at the time of this call is the timeout used for future member connect attempts. To use your own value call 'openReplicaSet'' instead.
 openReplicaSet rsSeed = readIORef globalConnectTimeout >>= flip openReplicaSet' rsSeed
 
 openReplicaSet' :: Secs -> (ReplicaSetName, [Host]) -> IO ReplicaSet
 -- ^ Open connections (on demand) to servers in replica set. Supplied hosts is seed list. At least one of them must be a live member of the named replica set, otherwise fail. Supplied seconds timeout is used for connect attempts to members.
-openReplicaSet' timeoutSecs (rsName, seedList) = do
+openReplicaSet' timeoutSecs (rs, hosts) = _openReplicaSet timeoutSecs (rs, hosts, Unsecure)
+
+openReplicaSetTLS :: (ReplicaSetName, [Host]) -> IO ReplicaSet
+-- ^ Open secure connections (on demand) to servers in the replica set. Supplied hosts is seed list. At least one of them must be a live member of the named replica set, otherwise fail. The value of 'globalConnectTimeout' at the time of this call is the timeout used for future member connect attempts. To use your own value call 'openReplicaSetTLS'' instead.
+openReplicaSetTLS  rsSeed = readIORef globalConnectTimeout >>= flip openReplicaSetTLS' rsSeed
+
+openReplicaSetTLS' :: Secs -> (ReplicaSetName, [Host]) -> IO ReplicaSet
+-- ^ Open secure connections (on demand) to servers in replica set. Supplied hosts is seed list. At least one of them must be a live member of the named replica set, otherwise fail. Supplied seconds timeout is used for connect attempts to members.
+openReplicaSetTLS' timeoutSecs (rs, hosts) = _openReplicaSet timeoutSecs (rs, hosts, Secure)
+
+_openReplicaSet :: Secs -> (ReplicaSetName, [Host], TransportSecurity) -> IO ReplicaSet
+_openReplicaSet timeoutSecs (rsName, seedList, transportSecurity) = do
     vMembers <- newMVar (map (, Nothing) seedList)
-    let rs = ReplicaSet rsName vMembers timeoutSecs
+    let rs = ReplicaSet rsName vMembers timeoutSecs transportSecurity
     _ <- updateMembers rs
     return rs
 
+openReplicaSetSRV :: HostName -> IO ReplicaSet
+-- ^ Open /non-secure/ connections (on demand) to servers in a replica set. The seedlist and replica set name is fetched from the SRV and TXT DNS records for the given hostname. The value of 'globalConnectTimeout' at the time of this call is the timeout used for future member connect attempts. To use your own value call 'openReplicaSetSRV''' instead.
+openReplicaSetSRV hostname = do
+    timeoutSecs <- readIORef globalConnectTimeout
+    _openReplicaSetSRV timeoutSecs Unsecure hostname
+
+openReplicaSetSRV' :: HostName -> IO ReplicaSet
+-- ^ Open /secure/ connections (on demand) to servers in a replica set. The seedlist and replica set name is fetched from the SRV and TXT DNS records for the given hostname. The value of 'globalConnectTimeout' at the time of this call is the timeout used for future member connect attempts. To use your own value call 'openReplicaSetSRV'''' instead.
+--
+-- The preferred connection method for cloud MongoDB providers. A typical connecting sequence is shown in the example below.
+--
+-- ==== __Example__
+-- >   do
+-- >   pipe <- openReplicatSetSRV' "cluster#.xxxxx.yyyyy.zzz"
+-- >   is_auth <- access pipe master "admin" $ auth user_name password
+-- >   unless is_auth (throwIO $ userError "Authentication failed!")
+openReplicaSetSRV' hostname = do
+    timeoutSecs <- readIORef globalConnectTimeout
+    _openReplicaSetSRV timeoutSecs Secure hostname
+
+openReplicaSetSRV'' :: Secs -> HostName -> IO ReplicaSet
+-- ^ Open /non-secure/ connections (on demand) to servers in a replica set. The seedlist and replica set name is fetched from the SRV and TXT DNS records for the given hostname. Supplied seconds timeout is used for connect attempts to members.
+openReplicaSetSRV'' timeoutSecs = _openReplicaSetSRV timeoutSecs Unsecure
+
+openReplicaSetSRV''' :: Secs -> HostName -> IO ReplicaSet
+-- ^ Open /secure/ connections (on demand) to servers in a replica set. The seedlist and replica set name is fetched from the SRV and TXT DNS records for the given hostname. Supplied seconds timeout is used for connect attempts to members.
+openReplicaSetSRV''' timeoutSecs = _openReplicaSetSRV timeoutSecs Secure
+
+_openReplicaSetSRV :: Secs -> TransportSecurity -> HostName -> IO ReplicaSet
+_openReplicaSetSRV timeoutSecs transportSecurity hostname = do
+    replicaSetName <- lookupReplicaSetName hostname
+    hosts <- lookupSeedList hostname
+    case (replicaSetName, hosts) of
+        (Nothing, _) -> throwError $ userError "Failed to lookup replica set name"
+        (_, [])  -> throwError $ userError "Failed to lookup replica set seedlist"
+        (Just rsName, _) ->
+            case transportSecurity of
+                Secure -> openReplicaSetTLS' timeoutSecs (rsName, hosts)
+                Unsecure -> openReplicaSet' timeoutSecs (rsName, hosts)
+
 closeReplicaSet :: ReplicaSet -> IO ()
 -- ^ Close all connections to replica set
-closeReplicaSet (ReplicaSet _ vMembers _) = withMVar vMembers $ mapM_ (maybe (return ()) close . snd)
+closeReplicaSet (ReplicaSet _ vMembers _ _) = withMVar vMembers $ mapM_ (maybe (return ()) close . snd)
 
 primary :: ReplicaSet -> IO Pipe
 -- ^ Return connection to current primary of replica set. Fail if no primary available.
-primary rs@(ReplicaSet rsName _ _) = do
+primary rs@(ReplicaSet rsName _ _ _) = do
     mHost <- statedPrimary <$> updateMembers rs
     case mHost of
         Just host' -> connection rs Nothing host'
@@ -172,7 +227,7 @@
 routedHost f rs = do
   info <- updateMembers rs
   hosts <- shuffle (possibleHosts info)
-  let addIsPrimary h = (h, if Just h == statedPrimary info then True else False)
+  let addIsPrimary h = (h, Just h == statedPrimary info)
   hosts' <- mergesortM (\a b -> f (addIsPrimary a) (addIsPrimary b)) hosts
   untilSuccess (connection rs Nothing) hosts'
 
@@ -189,7 +244,7 @@
 
 updateMembers :: ReplicaSet -> IO ReplicaInfo
 -- ^ Fetch replica info from any server and update members accordingly
-updateMembers rs@(ReplicaSet _ vMembers _) = do
+updateMembers rs@(ReplicaSet _ vMembers _ _) = do
     (host', info) <- untilSuccess (fetchReplicaInfo rs) =<< readMVar vMembers
     modifyMVar vMembers $ \members -> do
         let ((members', old), new) = intersection (map readHostPort $ at "hosts" info) members
@@ -203,7 +258,7 @@
 
 fetchReplicaInfo :: ReplicaSet -> (Host, Maybe Pipe) -> IO ReplicaInfo
 -- Connect to host and fetch replica info from host creating new connection if missing or closed (previously failed). Fail if not member of named replica set.
-fetchReplicaInfo rs@(ReplicaSet rsName _ _) (host', mPipe) = do
+fetchReplicaInfo rs@(ReplicaSet rsName _ _ _) (host', mPipe) = do
     pipe <- connection rs mPipe host'
     info <- adminCommand ["isMaster" =: (1 :: Int)] pipe
     case B.lookup "setName" info of
@@ -213,11 +268,15 @@
 
 connection :: ReplicaSet -> Maybe Pipe -> Host -> IO Pipe
 -- ^ Return new or existing connection to member of replica set. If pipe is already known for host it is given, but we still test if it is open.
-connection (ReplicaSet _ vMembers timeoutSecs) mPipe host' =
+connection (ReplicaSet _ vMembers timeoutSecs transportSecurity) mPipe host' =
     maybe conn (\p -> isClosed p >>= \bad -> if bad then conn else return p) mPipe
  where
     conn =  modifyMVar vMembers $ \members -> do
-        let new = connect' timeoutSecs host' >>= \pipe -> return (updateAssocs host' (Just pipe) members, pipe)
+        let (Host h p) = host'
+        let conn' = case transportSecurity of
+                        Secure   -> TLS.connect h p
+                        Unsecure -> connect' timeoutSecs host'
+        let new = conn' >>= \pipe -> return (updateAssocs host' (Just pipe) members, pipe)
         case List.lookup host' members of
             Just (Just pipe) -> isClosed pipe >>= \bad -> if bad then new else return (members, pipe)
             _ -> new
diff --git a/Database/MongoDB/GridFS.hs b/Database/MongoDB/GridFS.hs
--- a/Database/MongoDB/GridFS.hs
+++ b/Database/MongoDB/GridFS.hs
@@ -1,7 +1,7 @@
 -- Author:
 -- Brent Tubbs <brent.tubbs@gmail.com>
 -- | MongoDB GridFS implementation
-{-# LANGUAGE OverloadedStrings, RecordWildCards, NamedFieldPuns, TupleSections, FlexibleContexts, FlexibleInstances, UndecidableInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, StandaloneDeriving, TypeSynonymInstances, TypeFamilies, CPP, RankNTypes #-}
+{-# LANGUAGE OverloadedStrings, FlexibleContexts, FlexibleInstances, UndecidableInstances, MultiParamTypeClasses, TypeFamilies, CPP, RankNTypes #-}
 
 module Database.MongoDB.GridFS
   ( Bucket
@@ -23,11 +23,10 @@
   )
   where
 
-import Control.Applicative((<$>))
 
 import Control.Monad(when)
 import Control.Monad.IO.Class
-import Control.Monad.Trans(MonadTrans, lift)
+import Control.Monad.Trans(lift)
 
 import Data.Conduit
 import Data.Digest.Pure.MD5
@@ -63,17 +62,17 @@
 openBucket name = do
   let filesCollection = name `append` ".files"
   let chunksCollection = name `append` ".chunks"
-  ensureIndex $ (index filesCollection ["filename" =: (1::Int), "uploadDate" =: (1::Int)])
+  ensureIndex $ index filesCollection ["filename" =: (1::Int), "uploadDate" =: (1::Int)]
   ensureIndex $ (index chunksCollection ["files_id" =: (1::Int), "n" =: (1::Int)]) { iUnique = True, iDropDups = True }
   return $ Bucket filesCollection chunksCollection
 
 data File = File {bucket :: Bucket, document :: Document}
 
-getChunk :: (Monad m, MonadIO m) => File -> Int -> Action m (Maybe S.ByteString)
+getChunk :: (MonadFail m, MonadIO m) => File -> Int -> Action m (Maybe S.ByteString)
 -- ^ Get a chunk of a file
-getChunk (File bucket doc) i = do
+getChunk (File _bucket doc) i = do
   files_id <- B.look "_id" doc
-  result <- findOne $ select ["files_id" := files_id, "n" =: i] $ chunks bucket
+  result <- findOne $ select ["files_id" := files_id, "n" =: i] $ chunks _bucket
   let content = at "data" <$> result
   case content of
     Just (Binary b) -> return (Just b)
@@ -81,36 +80,36 @@
 
 findFile :: MonadIO m => Bucket -> Selector -> Action m [File]
 -- ^ Find files in the bucket
-findFile bucket sel = do
-  cursor <- find $ select sel $ files bucket
+findFile _bucket sel = do
+  cursor <- find $ select sel $ files _bucket
   results <- rest cursor
-  return $ File bucket <$> results
+  return $ File _bucket <$> results
 
 findOneFile :: MonadIO m => Bucket -> Selector -> Action m (Maybe File)
 -- ^ Find one file in the bucket
-findOneFile bucket sel = do
-  mdoc <- findOne $ select sel $ files bucket
-  return $ File bucket <$> mdoc
+findOneFile _bucket sel = do
+  mdoc <- findOne $ select sel $ files _bucket
+  return $ File _bucket <$> mdoc
 
 fetchFile :: MonadIO m => Bucket -> Selector -> Action m File
 -- ^ Fetch one file in the bucket
-fetchFile bucket sel = do
-  doc <- fetch $ select sel $ files bucket
-  return $ File bucket doc
+fetchFile _bucket sel = do
+  doc <- fetch $ select sel $ files _bucket
+  return $ File _bucket doc
 
-deleteFile :: (MonadIO m) => File -> Action m ()
+deleteFile :: (MonadIO m, MonadFail m) => File -> Action m ()
 -- ^ Delete files in the bucket
-deleteFile (File bucket doc) = do
+deleteFile (File _bucket doc) = do
   files_id <- B.look "_id" doc
-  delete $ select ["_id" := files_id] $ files bucket
-  delete $ select ["files_id" := files_id] $ chunks bucket
+  delete $ select ["_id" := files_id] $ files _bucket
+  delete $ select ["files_id" := files_id] $ chunks _bucket
 
 putChunk :: (Monad m, MonadIO m) => Bucket -> ObjectId -> Int -> L.ByteString -> Action m ()
 -- ^ Put a chunk in the bucket
-putChunk bucket files_id i chunk = do
-  insert_ (chunks bucket) ["files_id" =: files_id, "n" =: i, "data" =: Binary (L.toStrict chunk)]
+putChunk _bucket files_id i chunk = do
+  insert_ (chunks _bucket) ["files_id" =: files_id, "n" =: i, "data" =: Binary (L.toStrict chunk)]
 
-sourceFile :: (Monad m, MonadIO m) => File -> Producer (Action m) S.ByteString
+sourceFile :: (MonadFail m, MonadIO m) => File -> ConduitT File S.ByteString (Action m) ()
 -- ^ A producer for the contents of a file
 sourceFile file = yieldChunk 0 where
   yieldChunk i = do
@@ -121,69 +120,69 @@
 
 -- Used to keep data during writing
 data FileWriter = FileWriter
-  { fwChunkSize :: Int64
-  , fwBucket :: Bucket
-  , fwFilesId :: ObjectId
-  , fwChunkIndex :: Int
-  , fwSize :: Int64
-  , fwAcc :: L.ByteString
-  , fwMd5Context :: MD5Context
-  , fwMd5acc :: L.ByteString
+  { _fwChunkSize :: Int64
+  , _fwBucket :: Bucket
+  , _fwFilesId :: ObjectId
+  , _fwChunkIndex :: Int
+  , _fwSize :: Int64
+  , _fwAcc :: L.ByteString
+  , _fwMd5Context :: MD5Context
+  , _fwMd5acc :: L.ByteString
   }
 
 -- Finalize file, calculating md5 digest, saving the last chunk, and creating the file in the bucket
 finalizeFile :: (Monad m, MonadIO m) => Text -> FileWriter -> Action m File
-finalizeFile filename (FileWriter chunkSize bucket files_id i size acc md5context md5acc) = do
+finalizeFile filename (FileWriter chunkSize _bucket files_id i size acc md5context md5acc) = do
   let md5digest = finalizeMD5 md5context (L.toStrict md5acc)
-  when (L.length acc > 0) $ putChunk bucket files_id i acc
-  timestamp <- liftIO $ getCurrentTime
+  when (L.length acc > 0) $ putChunk _bucket files_id i acc
+  currentTimestamp <- liftIO getCurrentTime
   let doc = [ "_id" =: files_id
             , "length" =: size
-            , "uploadDate" =: timestamp
-            , "md5" =: show (md5digest)
+            , "uploadDate" =: currentTimestamp
+            , "md5" =: show md5digest
             , "chunkSize" =: chunkSize
             , "filename" =: filename
             ]
-  insert_ (files bucket) doc
-  return $ File bucket doc
+  insert_ (files _bucket) doc
+  return $ File _bucket doc
 
 -- finalize the remainder and return the MD5Digest.
 finalizeMD5 :: MD5Context -> S.ByteString -> MD5Digest
-finalizeMD5 ctx rest =
-  md5Finalize ctx2 (S.drop lu rest) -- can only handle max md5BlockSizeInBytes length
+finalizeMD5 ctx remainder =
+  md5Finalize ctx2 (S.drop lu remainder) -- can only handle max md5BlockSizeInBytes length
   where
-    l = S.length rest
+    l = S.length remainder
     r = l `mod` md5BlockSizeInBytes
     lu = l - r
-    ctx2 = md5Update ctx (S.take lu rest)
+    ctx2 = md5Update ctx (S.take lu remainder)
 
 -- Write as many chunks as can be written from the file writer
 writeChunks :: (Monad m, MonadIO m) => FileWriter -> L.ByteString -> Action m FileWriter
-writeChunks (FileWriter chunkSize bucket files_id i size acc md5context md5acc) chunk = do
+writeChunks (FileWriter chunkSize _bucket files_id i size acc md5context md5acc) chunk = do
   -- Update md5 context
   let md5BlockLength = fromIntegral $ untag (blockLength :: Tagged MD5Digest Int)
   let md5acc_temp = (md5acc `L.append` chunk)
-  let (md5context', md5acc') = 
+  let (md5context', md5acc') =
         if (L.length md5acc_temp < md5BlockLength)
         then (md5context, md5acc_temp)
         else let numBlocks = L.length md5acc_temp `div` md5BlockLength
-                 (current, rest) = L.splitAt (md5BlockLength * numBlocks) md5acc_temp
-             in (md5Update md5context (L.toStrict current), rest)
+                 (current, remainder) = L.splitAt (md5BlockLength * numBlocks) md5acc_temp
+             in (md5Update md5context (L.toStrict current), remainder)
   -- Update chunks
   let size' = (size + L.length chunk)
   let acc_temp = (acc `L.append` chunk)
   if (L.length acc_temp < chunkSize)
-    then return (FileWriter chunkSize bucket files_id i size' acc_temp md5context' md5acc')
+    then return (FileWriter chunkSize _bucket files_id i size' acc_temp md5context' md5acc')
     else do
-      let (chunk, acc') = L.splitAt chunkSize acc_temp
-      putChunk bucket files_id i chunk
-      writeChunks (FileWriter chunkSize bucket files_id (i+1) size' acc' md5context' md5acc') L.empty
+      let (newChunk, acc') = L.splitAt chunkSize acc_temp
+      putChunk _bucket files_id i newChunk
+      writeChunks (FileWriter chunkSize _bucket files_id (i+1) size' acc' md5context' md5acc') L.empty
 
-sinkFile :: (Monad m, MonadIO m) => Bucket -> Text -> Consumer S.ByteString (Action m) File
+sinkFile :: (Monad m, MonadIO m) => Bucket -> Text -> ConduitT S.ByteString () (Action m) File
 -- ^ A consumer that creates a file in the bucket and puts all consumed data in it
-sinkFile bucket filename = do
+sinkFile _bucket filename = do
   files_id <- liftIO $ genObjectId
-  awaitChunk $ FileWriter defaultChunkSize bucket files_id 0 0 L.empty md5InitialContext L.empty
+  awaitChunk $ FileWriter defaultChunkSize _bucket files_id 0 0 L.empty md5InitialContext L.empty
  where
   awaitChunk fw = do
     mchunk <- await
diff --git a/Database/MongoDB/Internal/Network.hs b/Database/MongoDB/Internal/Network.hs
new file mode 100644
--- /dev/null
+++ b/Database/MongoDB/Internal/Network.hs
@@ -0,0 +1,106 @@
+-- | Compatibility layer for network package, including newtype 'PortID'
+{-# LANGUAGE CPP, OverloadedStrings #-}
+
+module Database.MongoDB.Internal.Network (Host(..), PortID(..), N.HostName, connectTo, 
+                                          lookupReplicaSetName, lookupSeedList) where
+
+#if !MIN_VERSION_network(2, 9, 0)
+
+import qualified Network as N
+import System.IO (Handle)
+
+#else
+
+import Control.Exception (bracketOnError)
+import Network.BSD as BSD
+import qualified Network.Socket as N
+import System.IO (Handle, IOMode(ReadWriteMode))
+
+#endif
+
+import Data.ByteString.Char8 (pack, unpack)
+import Data.List (dropWhileEnd)
+import Data.Maybe (fromMaybe)
+import Data.Text (Text)
+import Network.DNS.Lookup (lookupSRV, lookupTXT)
+import Network.DNS.Resolver (defaultResolvConf, makeResolvSeed, withResolver)
+import Network.HTTP.Types.URI (parseQueryText)
+
+
+-- | Wraps network's 'PortNumber'
+-- Used to ease compatibility between older and newer network versions.
+data PortID = PortNumber N.PortNumber
+#if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS) && !defined(_WIN32)
+            | UnixSocket String
+#endif
+            deriving (Eq, Ord, Show)
+
+
+#if !MIN_VERSION_network(2, 9, 0)
+
+-- Unwrap our newtype and use network's PortID and connectTo
+connectTo :: N.HostName         -- Hostname
+          -> PortID             -- Port Identifier
+          -> IO Handle          -- Connected Socket
+connectTo hostname (PortNumber port) = N.connectTo hostname (N.PortNumber port)
+
+#if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS) && !defined(_WIN32)
+connectTo _ (UnixSocket path) = N.connectTo "" (N.UnixSocket path)
+#endif
+
+#else
+
+-- Copied implementation from network 2.8's 'connectTo', but using our 'PortID' newtype.
+-- https://github.com/haskell/network/blob/e73f0b96c9da924fe83f3c73488f7e69f712755f/Network.hs#L120-L129
+connectTo :: N.HostName         -- Hostname
+          -> PortID             -- Port Identifier
+          -> IO Handle          -- Connected Socket
+connectTo hostname (PortNumber port) = do
+    proto <- BSD.getProtocolNumber "tcp"
+    bracketOnError
+        (N.socket N.AF_INET N.Stream proto)
+        N.close  -- only done if there's an error
+        (\sock -> do
+          he <- BSD.getHostByName hostname
+          N.connect sock (N.SockAddrInet port (hostAddress he))
+          N.socketToHandle sock ReadWriteMode
+        )
+
+#if !defined(mingw32_HOST_OS) && !defined(cygwin32_HOST_OS) && !defined(_WIN32)
+connectTo _ (UnixSocket path) = do
+    bracketOnError
+        (N.socket N.AF_UNIX N.Stream 0)
+        N.close
+        (\sock -> do
+          N.connect sock (N.SockAddrUnix path)
+          N.socketToHandle sock ReadWriteMode
+        )
+#endif
+
+#endif
+
+-- * Host
+
+data Host = Host N.HostName PortID  deriving (Show, Eq, Ord)
+
+lookupReplicaSetName :: N.HostName -> IO (Maybe Text)
+-- ^ Retrieves the replica set name from the TXT DNS record for the given hostname
+lookupReplicaSetName hostname = do 
+  rs <- makeResolvSeed defaultResolvConf
+  res <- withResolver rs $ \resolver -> lookupTXT resolver (pack hostname)
+  case res of 
+    Left _ -> pure Nothing 
+    Right [] -> pure Nothing 
+    Right (x:_) ->
+      pure $ fromMaybe (Nothing :: Maybe Text) (lookup "replicaSet" $ parseQueryText x)
+
+lookupSeedList :: N.HostName -> IO [Host]
+-- ^ Retrieves the replica set seed list from the SRV DNS record for the given hostname
+lookupSeedList hostname = do 
+  rs <- makeResolvSeed defaultResolvConf
+  res <- withResolver rs $ \resolver -> lookupSRV resolver $ pack $ "_mongodb._tcp." ++ hostname
+  case res of 
+    Left _ -> pure []
+    Right srv -> pure $ map (\(_, _, por, tar) -> 
+      let tar' = dropWhileEnd (=='.') (unpack tar) 
+      in Host tar' (PortNumber . fromIntegral $ por)) srv
diff --git a/Database/MongoDB/Internal/Protocol.hs b/Database/MongoDB/Internal/Protocol.hs
--- a/Database/MongoDB/Internal/Protocol.hs
+++ b/Database/MongoDB/Internal/Protocol.hs
@@ -4,8 +4,8 @@
 -- This module is not intended for direct use. Use the high-level interface at
 -- "Database.MongoDB.Query" and "Database.MongoDB.Connection" instead.
 
-{-# LANGUAGE RecordWildCards, StandaloneDeriving, OverloadedStrings #-}
-{-# LANGUAGE CPP, FlexibleContexts, TupleSections, TypeSynonymInstances #-}
+{-# LANGUAGE RecordWildCards, OverloadedStrings #-}
+{-# LANGUAGE CPP, FlexibleContexts #-}
 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, UndecidableInstances #-}
 {-# LANGUAGE BangPatterns #-}
 
@@ -20,42 +20,43 @@
 module Database.MongoDB.Internal.Protocol (
     FullCollection,
     -- * Pipe
-    Pipe, newPipe, newPipeWith, send, call,
+    Pipe,  newPipe, newPipeWith, send, sendOpMsg, call, callOpMsg,
     -- ** Notice
     Notice(..), InsertOption(..), UpdateOption(..), DeleteOption(..), CursorId,
     -- ** Request
-    Request(..), QueryOption(..),
+    Request(..), QueryOption(..), Cmd (..), KillC(..),
     -- ** Reply
-    Reply(..), ResponseFlag(..),
+    Reply(..), ResponseFlag(..), FlagBit(..),
     -- * Authentication
     Username, Password, Nonce, pwHash, pwKey,
-    isClosed, close, ServerData(..), Pipeline(..)
+    isClosed, close, ServerData(..), Pipeline(..), putOpMsg,
+    bitOpMsg
 ) where
 
 #if !MIN_VERSION_base(4,8,0)
 import Control.Applicative ((<$>))
 #endif
-import Control.Monad (forM, replicateM, unless)
-import Data.Binary.Get (Get, runGet)
-import Data.Binary.Put (Put, runPut)
-import Data.Bits (bit, testBit)
+import Control.Monad ( forM, replicateM, unless, forever )
+import Data.Binary.Get (Get, runGet, getInt8)
+import Data.Binary.Put (Put, runPut, putInt8)
+import Data.Bits (bit, testBit, zeroBits)
 import Data.Int (Int32, Int64)
 import Data.IORef (IORef, newIORef, atomicModifyIORef)
 import System.IO (Handle)
 import System.IO.Error (doesNotExistErrorType, mkIOError)
 import System.IO.Unsafe (unsafePerformIO)
-import Data.Maybe (maybeToList)
+import Data.Maybe (maybeToList, fromJust)
 import GHC.Conc (ThreadStatus(..), threadStatus)
-import Control.Monad (forever)
-import Control.Concurrent.Chan (Chan, newChan, readChan, writeChan, isEmptyChan)
-import Control.Concurrent (ThreadId, killThread, forkFinally)
+import Control.Monad.STM (atomically)
+import Control.Concurrent (ThreadId, killThread, forkIOWithUnmask)
+import Control.Concurrent.STM.TChan (TChan, newTChan, readTChan, writeTChan, isEmptyTChan)
 
-import Control.Exception.Lifted (onException, throwIO, try)
+import Control.Exception.Lifted (SomeException, mask_, onException, throwIO, try)
 
 import qualified Data.ByteString.Lazy as L
 
 import Control.Monad.Trans (MonadIO, liftIO)
-import Data.Bson (Document)
+import Data.Bson (Document, (=:), merge, cast, valueAt, look)
 import Data.Bson.Binary (getDocument, putDocument, getInt32, putInt32, getInt64,
                          putInt64, putCString)
 import Data.Text (Text)
@@ -69,9 +70,12 @@
 import Database.MongoDB.Transport (Transport)
 import qualified Database.MongoDB.Transport as Tr
 
+
 #if MIN_VERSION_base(4,6,0)
 import Control.Concurrent.MVar.Lifted (MVar, newEmptyMVar, newMVar, withMVar,
                                        putMVar, readMVar, mkWeakMVar, isEmptyMVar)
+import GHC.List (foldl1')
+import Conduit (repeatWhileMC, (.|), runConduit, foldlC)
 #else
 import Control.Concurrent.MVar.Lifted (MVar, newEmptyMVar, newMVar, withMVar,
                                          putMVar, readMVar, addMVarFinalizer)
@@ -82,12 +86,13 @@
 mkWeakMVar = addMVarFinalizer
 #endif
 
+
 -- * Pipeline
 
 -- | Thread-safe and pipelined connection
 data Pipeline = Pipeline
     { vStream :: MVar Transport -- ^ Mutex on handle, so only one thread at a time can write to it
-    , responseQueue :: Chan (MVar (Either IOError Response)) -- ^ Queue of threads waiting for responses. Every time a response arrive we pop the next thread and give it the response.
+    , responseQueue :: TChan (MVar (Either IOError Response)) -- ^ Queue of threads waiting for responses. Every time a response arrives we pop the next thread and give it the response.
     , listenThread :: ThreadId
     , finished :: MVar ()
     , serverData :: ServerData
@@ -101,19 +106,26 @@
                 , maxBsonObjectSize   :: Int
                 , maxWriteBatchSize   :: Int
                 }
+                deriving Show
 
+-- | @'forkUnmaskedFinally' action and_then@ behaves the same as @'forkFinally' action and_then@, except that @action@ is run completely unmasked, whereas with 'forkFinally', @action@ is run with the same mask as the parent thread.
+forkUnmaskedFinally :: IO a -> (Either SomeException a -> IO ()) -> IO ThreadId
+forkUnmaskedFinally action and_then =
+  mask_ $ forkIOWithUnmask $ \unmask ->
+    try (unmask action) >>= and_then
+
 -- | Create new Pipeline over given handle. You should 'close' pipeline when finished, which will also close handle. If pipeline is not closed but eventually garbage collected, it will be closed along with handle.
 newPipeline :: ServerData -> Transport -> IO Pipeline
 newPipeline serverData stream = do
     vStream <- newMVar stream
-    responseQueue <- newChan
+    responseQueue <- atomically newTChan
     finished <- newEmptyMVar
     let drainReplies = do
-          chanEmpty <- isEmptyChan responseQueue
+          chanEmpty <- atomically $ isEmptyTChan responseQueue
           if chanEmpty
             then return ()
             else do
-              var <- readChan responseQueue
+              var <- atomically $ readTChan responseQueue
               putMVar var $ Left $ mkIOError
                                         doesNotExistErrorType
                                         "Handle has been closed"
@@ -123,9 +135,9 @@
 
     rec
         let pipe = Pipeline{..}
-        listenThread <- forkFinally (listen pipe) $ \_ -> do
-                                                       putMVar finished ()
-                                                       drainReplies
+        listenThread <- forkUnmaskedFinally (listen pipe) $ \_ -> do
+                                                              putMVar finished ()
+                                                              drainReplies
 
     _ <- mkWeakMVar vStream $ do
         killThread listenThread
@@ -151,6 +163,7 @@
         ThreadFinished -> True
         ThreadBlocked _ -> False
         ThreadDied -> True
+
 --isPipeClosed Pipeline{..} = isClosed =<< readMVar vHandle  -- isClosed hangs while listen loop is waiting on read
 
 listen :: Pipeline -> IO ()
@@ -159,7 +172,7 @@
     stream <- readMVar vStream
     forever $ do
         e <- try $ readMessage stream
-        var <- readChan responseQueue
+        var <- atomically $ readTChan responseQueue
         putMVar var e
         case e of
             Left err -> Tr.close stream >> ioError err  -- close and stop looping
@@ -170,6 +183,14 @@
 -- Throw IOError and close pipeline if send fails
 psend p@Pipeline{..} !message = withMVar vStream (flip writeMessage message) `onException` close p
 
+psendOpMsg :: Pipeline -> [Cmd] -> Maybe FlagBit -> Document -> IO ()-- IO (IO Response)
+psendOpMsg p@Pipeline{..} commands flagBit params =
+  case flagBit of
+    Just f -> case f of
+               MoreToCome -> withMVar vStream (\t -> writeOpMsgMessage t (commands, Nothing) flagBit params) `onException` close p -- >> return (return (0, ReplyEmpty))
+               _ -> error "moreToCome has to be set if no response is expected"
+    _ -> error "moreToCome has to be set if no response is expected"
+
 pcall :: Pipeline -> Message -> IO (IO Response)
 -- ^ Send message to destination and return /promise/ of response from one message only. The destination must reply to the message (otherwise promises will have the wrong responses in them).
 -- Throw IOError and closes pipeline if send fails, likewise for promised response.
@@ -182,13 +203,31 @@
     doCall stream = do
         writeMessage stream message
         var <- newEmptyMVar
-        liftIO $ writeChan responseQueue var
+        liftIO $ atomically $ writeTChan responseQueue var
         return $ readMVar var >>= either throwIO return -- return promise
 
+pcallOpMsg :: Pipeline -> Maybe (Request, RequestId) -> Maybe FlagBit -> Document -> IO (IO Response)
+-- ^ Send message to destination and return /promise/ of response from one message only. The destination must reply to the message (otherwise promises will have the wrong responses in them).
+-- Throw IOError and closes pipeline if send fails, likewise for promised response.
+pcallOpMsg p@Pipeline{..} message flagbit params = do
+  listenerStopped <- isFinished p
+  if listenerStopped
+    then ioError $ mkIOError doesNotExistErrorType "Handle has been closed" Nothing Nothing
+    else withMVar vStream doCall `onException` close p
+  where
+    doCall stream = do
+        writeOpMsgMessage stream ([], message) flagbit params
+        var <- newEmptyMVar
+        -- put var into the response-queue so that it can
+        -- fetch the latest response
+        liftIO $ atomically $ writeTChan responseQueue var
+        return $ readMVar var >>= either throwIO return -- return promise
+
 -- * Pipe
 
 type Pipe = Pipeline
--- ^ Thread-safe TCP connection with pipelined requests
+-- ^ Thread-safe TCP connection with pipelined requests. In long-running applications the user is expected to use it as a "client": create a `Pipe`
+-- at startup, use it as long as possible, watch out for possible timeouts, and close it on shutdown. Bearing in mind that disconnections may be triggered by MongoDB service providers, the user is responsible for re-creating their `Pipe` whenever necessary.
 
 newPipe :: ServerData -> Handle -> IO Pipe
 -- ^ Create pipe over handle
@@ -202,6 +241,12 @@
 -- ^ Send notices as a contiguous batch to server with no reply. Throw IOError if connection fails.
 send pipe notices = psend pipe (notices, Nothing)
 
+sendOpMsg :: Pipe -> [Cmd] -> Maybe FlagBit -> Document -> IO ()
+-- ^ Send notices as a contiguous batch to server with no reply. Throw IOError if connection fails.
+sendOpMsg pipe commands@(Nc _ : _) flagBit params =  psendOpMsg pipe commands flagBit params
+sendOpMsg pipe commands@(Kc _ : _) flagBit params =  psendOpMsg pipe commands flagBit params
+sendOpMsg _ _ _ _ =  error "This function only supports Cmd types wrapped in Nc or Kc type constructors"
+
 call :: Pipe -> [Notice] -> Request -> IO (IO Reply)
 -- ^ Send notices and request as a contiguous batch to server and return reply promise, which will block when invoked until reply arrives. This call and resulting promise will throw IOError if connection fails.
 call pipe notices request = do
@@ -212,11 +257,73 @@
     check requestId (responseTo, reply) = if requestId == responseTo then reply else
         error $ "expected response id (" ++ show responseTo ++ ") to match request id (" ++ show requestId ++ ")"
 
+callOpMsg :: Pipe -> Request -> Maybe FlagBit -> Document -> IO (IO Reply)
+-- ^ Send requests as a contiguous batch to server and return reply promise, which will block when invoked until reply arrives. This call and resulting promise will throw IOError if connection fails.
+callOpMsg pipe request flagBit params = do
+    requestId <- genRequestId
+    promise <- pcallOpMsg pipe (Just (request, requestId)) flagBit params
+    promise' <- promise :: IO Response
+    return $ snd <$> produce requestId promise'
+ where
+   -- We need to perform streaming here as within the OP_MSG protocol mongoDB expects
+   -- our client to keep receiving messages after the MoreToCome flagbit was
+   -- set by the server until our client receives an empty flagbit. After the
+   -- first MoreToCome flagbit was set the responseTo field in the following
+   -- headers will reference the cursorId that was set in the previous message.
+   -- see:
+   -- https://github.com/mongodb/specifications/blob/master/source/message/OP_MSG.rst#moretocome-on-responses
+    checkFlagBit p =
+      case p of
+        (_, r) ->
+          case r of
+            ReplyOpMsg{..} -> flagBits == [MoreToCome]
+             -- This is called by functions using the OP_MSG protocol,
+             -- so this has to be ReplyOpMsg
+            _ -> error "Impossible"
+    produce reqId p = runConduit $
+      case p of
+        (rt, r) ->
+          case r of
+              ReplyOpMsg{..} ->
+                if flagBits == [MoreToCome]
+                  then yieldResponses .| foldlC mergeResponses p
+                  else return $ (rt, check reqId p)
+              _ -> error "Impossible" -- see comment above
+    yieldResponses = repeatWhileMC
+          (do
+             var <- newEmptyMVar
+             liftIO $ atomically $ writeTChan (responseQueue pipe) var
+             readMVar var >>= either throwIO return :: IO Response
+          )
+          checkFlagBit
+    mergeResponses p@(rt,rep) p' =
+      case (p, p') of
+          ((_, r), (_, r')) ->
+            case (r, r') of
+                (ReplyOpMsg _ sec _, ReplyOpMsg _ sec' _) -> do
+                    let (section, section') = (head sec, head sec')
+                        (cur, cur') = (maybe Nothing cast $ look "cursor" section,
+                                      maybe Nothing cast $ look "cursor" section')
+                    case (cur, cur') of
+                      (Just doc, Just doc') -> do
+                        let (docs, docs') =
+                              ( fromJust $ cast $ valueAt "nextBatch" doc :: [Document]
+                              , fromJust $ cast $ valueAt "nextBatch" doc' :: [Document])
+                            id' = fromJust $ cast $ valueAt "id" doc' :: Int32
+                        (rt, check id' (rt, rep{ sections = docs' ++ docs })) -- todo: avoid (++)
+                        -- Since we use this to process moreToCome messages, we
+                        -- know that there will be a nextBatch key in the document
+                      _ ->  error "Impossible"
+                _ -> error "Impossible" -- see comment above
+    check requestId (responseTo, reply) = if requestId == responseTo then reply else
+        error $ "expected response id (" ++ show responseTo ++ ") to match request id (" ++ show requestId ++ ")"
+
 -- * Message
 
 type Message = ([Notice], Maybe (Request, RequestId))
 -- ^ A write notice(s) with getLastError request, or just query request.
 -- Note, that requestId will be out of order because request ids will be generated for notices after the request id supplied was generated. This is ok because the mongo server does not care about order just uniqueness.
+type OpMsgMessage = ([Cmd], Maybe (Request, RequestId))
 
 writeMessage :: Transport -> Message -> IO ()
 -- ^ Write message to connection
@@ -237,6 +344,25 @@
     lenBytes bytes = encodeSize . toEnum . fromEnum $ L.length bytes
     encodeSize = runPut . putInt32 . (+ 4)
 
+writeOpMsgMessage :: Transport -> OpMsgMessage -> Maybe FlagBit -> Document -> IO ()
+-- ^ Write message to connection
+writeOpMsgMessage conn (notices, mRequest) flagBit params = do
+    noticeStrings <- forM notices $ \n -> do
+          requestId <- genRequestId
+          let s = runPut $ putOpMsg n requestId flagBit params
+          return $ (lenBytes s) `L.append` s
+
+    let requestString = do
+           (request, requestId) <- mRequest
+           let s = runPut $ putOpMsg (Req request) requestId flagBit params
+           return $ (lenBytes s) `L.append` s
+
+    Tr.write conn $ L.toStrict $ L.concat $ noticeStrings ++ (maybeToList requestString)
+    Tr.flush conn
+ where
+    lenBytes bytes = encodeSize . toEnum . fromEnum $ L.length bytes
+    encodeSize = runPut . putInt32 . (+ 4)
+
 type Response = (ResponseTo, Reply)
 -- ^ Message received from a Mongo server in response to a Request
 
@@ -262,6 +388,7 @@
 
 genRequestId :: (MonadIO m) => m RequestId
 -- ^ Generate fresh request id
+{-# NOINLINE genRequestId #-}
 genRequestId = liftIO $ atomicModifyIORef counter $ \n -> (n + 1, n) where
     counter :: IORef RequestId
     counter = unsafePerformIO (newIORef 0)
@@ -276,6 +403,13 @@
     putInt32 0
     putInt32 opcode
 
+putOpMsgHeader :: Opcode -> RequestId -> Put
+-- ^ Note, does not write message length (first int32), assumes caller will write it
+putOpMsgHeader opcode requestId = do
+    putInt32 requestId
+    putInt32 0
+    putInt32 opcode
+
 getHeader :: Get (Opcode, ResponseTo)
 -- ^ Note, does not read message length (first int32), assumes it was already read
 getHeader = do
@@ -350,6 +484,144 @@
             putInt32 $ toEnum (length kCursorIds)
             mapM_ putInt64 kCursorIds
 
+data KillC = KillC { killCursor :: Notice, kFullCollection:: FullCollection} deriving Show
+
+data Cmd = Nc Notice | Req Request | Kc KillC deriving Show
+
+data FlagBit =
+      ChecksumPresent  -- ^ The message ends with 4 bytes containing a CRC-32C checksum
+    | MoreToCome  -- ^ Another message will follow this one without further action from the receiver.
+    | ExhaustAllowed  -- ^ The client is prepared for multiple replies to this request using the moreToCome bit.
+    deriving (Show, Eq, Enum)
+
+uOptDoc :: UpdateOption -> Document
+uOptDoc Upsert = ["upsert" =: True]
+uOptDoc MultiUpdate = ["multi" =: True]
+
+{-
+  OP_MSG header == 16 byte
+  + 4 bytes flagBits
+  + 1 byte payload type = 1
+  + 1 byte payload type = 2
+  + 4 byte size of payload
+  == 26 bytes opcode overhead
+  + X Full command document {insert: "test", writeConcern: {...}}
+  + Y command identifier ("documents", "deletes", "updates") ( + \0)
+-}
+putOpMsg :: Cmd -> RequestId -> Maybe FlagBit -> Document -> Put
+putOpMsg cmd requestId flagBit params = do
+    let biT = maybe zeroBits (bit . bitOpMsg) flagBit:: Int32
+    putOpMsgHeader opMsgOpcode requestId -- header
+    case cmd of
+        Nc n -> case n of
+            Insert{..} -> do
+                let (sec0, sec1Size) =
+                      prepSectionInfo
+                          iFullCollection
+                          (Just (iDocuments:: [Document]))
+                          (Nothing:: Maybe Document)
+                          ("insert":: Text)
+                          ("documents":: Text)
+                          params
+                putInt32 biT                         -- flagBit
+                putInt8 0                            -- payload type 0
+                putDocument sec0                     -- payload
+                putInt8 1                            -- payload type 1
+                putInt32 sec1Size                    -- size of section
+                putCString "documents"               -- identifier
+                mapM_ putDocument iDocuments         -- payload
+            Update{..} -> do
+                let doc = ["q" =: uSelector, "u" =: uUpdater] <> concatMap uOptDoc uOptions
+                    (sec0, sec1Size) =
+                      prepSectionInfo
+                          uFullCollection
+                          (Nothing:: Maybe [Document])
+                          (Just doc)
+                          ("update":: Text)
+                          ("updates":: Text)
+                          params
+                putInt32 biT
+                putInt8 0
+                putDocument sec0
+                putInt8 1
+                putInt32 sec1Size
+                putCString "updates"
+                putDocument doc
+            Delete{..} -> do
+                -- Setting limit to 1 here is ok, since this is only used by deleteOne
+                let doc = ["q" =: dSelector, "limit" =: (1 :: Int32)]
+                    (sec0, sec1Size) =
+                      prepSectionInfo
+                          dFullCollection
+                          (Nothing:: Maybe [Document])
+                          (Just doc)
+                          ("delete":: Text)
+                          ("deletes":: Text)
+                          params
+                putInt32 biT
+                putInt8 0
+                putDocument sec0
+                putInt8 1
+                putInt32 sec1Size
+                putCString "deletes"
+                putDocument doc
+            _ -> error "The KillCursors command cannot be wrapped into a Nc type constructor. Please use the Kc type constructor"
+        Req r -> case r of
+            Query{..} -> do
+                let n = T.splitOn "." qFullCollection
+                    db = head n
+                    sec0 = foldl1' merge [qProjector, [ "$db" =: db ], qSelector]
+                putInt32 biT
+                putInt8 0
+                putDocument sec0
+            GetMore{..} -> do
+                let n = T.splitOn "." gFullCollection
+                    (db, coll) = (head n, last n)
+                    pre = ["getMore" =: gCursorId, "collection" =: coll, "$db" =: db, "batchSize" =: gBatchSize]
+                putInt32 (bit $ bitOpMsg $ ExhaustAllowed)
+                putInt8 0
+                putDocument pre
+            Message{..} -> do
+                putInt32 biT
+                putInt8 0
+                putDocument $ merge [ "$db" =: mDatabase ] mParams
+        Kc k -> case k of
+            KillC{..} -> do
+                let n = T.splitOn "." kFullCollection
+                    (db, coll) = (head n, last n)
+                case killCursor of
+                  KillCursors{..} -> do
+                      let doc = ["killCursors" =: coll, "cursors" =: kCursorIds, "$db" =: db]
+                      putInt32 biT
+                      putInt8 0
+                      putDocument doc
+                  -- Notices are already captured at the beginning, so all
+                  -- other cases are impossible
+                  _ -> error "impossible"
+ where
+    lenBytes bytes = toEnum . fromEnum $ L.length bytes:: Int32
+    prepSectionInfo fullCollection documents document command identifier ps =
+      let n = T.splitOn "." fullCollection
+          (db, coll) = (head n, last n)
+      in
+      case documents of
+        Just ds ->
+            let
+                sec0 = merge ps [command =: coll, "$db" =: db]
+                s = sum $ map (lenBytes . runPut . putDocument) ds
+                i = runPut $ putCString identifier
+                -- +4 bytes for the type 1 section size that has to be
+                -- transported in addition to the type 1 section document
+                sec1Size = s + lenBytes i + 4
+            in (sec0, sec1Size)
+        Nothing ->
+            let
+                sec0 = merge ps [command =: coll, "$db" =: db]
+                s = runPut $ putDocument $ fromJust document
+                i = runPut $ putCString identifier
+                sec1Size = lenBytes s + lenBytes i + 4
+            in (sec0, sec1Size)
+
 iBit :: InsertOption -> Int32
 iBit KeepGoing = bit 0
 
@@ -369,6 +641,11 @@
 dBits :: [DeleteOption] -> Int32
 dBits = bitOr . map dBit
 
+bitOpMsg :: FlagBit -> Int
+bitOpMsg ChecksumPresent = 0
+bitOpMsg MoreToCome = 1
+bitOpMsg ExhaustAllowed = 16
+
 -- ** Request
 
 -- | A request is a message that is sent with a 'Reply' expected in return
@@ -378,22 +655,28 @@
         qFullCollection :: FullCollection,
         qSkip :: Int32,  -- ^ Number of initial matching documents to skip
         qBatchSize :: Int32,  -- ^ The number of document to return in each batch response from the server. 0 means use Mongo default. Negative means close cursor after first batch and use absolute value as batch size.
-        qSelector :: Document,  -- ^ \[\] = return all documents in collection
-        qProjector :: Document  -- ^ \[\] = return whole document
+        qSelector :: Document,  -- ^ @[]@ = return all documents in collection
+        qProjector :: Document  -- ^ @[]@ = return whole document
     } | GetMore {
         gFullCollection :: FullCollection,
         gBatchSize :: Int32,
-        gCursorId :: CursorId}
+        gCursorId :: CursorId
+    } | Message {
+        mDatabase :: Text,
+        mParams :: Document
+    }
     deriving (Show, Eq)
 
 data QueryOption =
-      TailableCursor  -- ^ Tailable means cursor is not closed when the last data is retrieved. Rather, the cursor marks the final object's position. You can resume using the cursor later, from where it was located, if more data were received. Like any "latent cursor", the cursor may become invalid at some point – for example if the final object it references were deleted. Thus, you should be prepared to requery on CursorNotFound exception.
+      TailableCursor  -- ^ Tailable means cursor is not closed when the last data is retrieved. Rather, the cursor marks the final object's position. You can resume using the cursor later, from where it was located, if more data were received. Like any "latent cursor", the cursor may become invalid at some point – for example if the final object it references were deleted. Thus, you should be prepared to requery on @CursorNotFound@ exception.
     | SlaveOK  -- ^ Allow query of replica slave. Normally these return an error except for namespace "local".
     | NoCursorTimeout  -- ^ The server normally times out idle cursors after 10 minutes to prevent a memory leak in case a client forgets to close a cursor. Set this option to allow a cursor to live forever until it is closed.
     | AwaitData  -- ^ Use with TailableCursor. If we are at the end of the data, block for a while rather than returning no data. After a timeout period, we do return as normal.
+
 --  | Exhaust  -- ^ Stream the data down full blast in multiple "more" packages, on the assumption that the client will fully read all data queried. Faster when you are pulling a lot of data and know you want to pull it all down. Note: the client is not allowed to not read all the data unless it closes the connection.
 -- Exhaust commented out because not compatible with current `Pipeline` implementation
-    | Partial  -- ^ Get partial results from a _mongos_ if some shards are down, instead of throwing an error.
+
+    | Partial  -- ^ Get partial results from a /mongos/ if some shards are down, instead of throwing an error.
     deriving (Show, Eq)
 
 -- *** Binary format
@@ -401,7 +684,11 @@
 qOpcode :: Request -> Opcode
 qOpcode Query{} = 2004
 qOpcode GetMore{} = 2005
+qOpcode Message{} = 2013
 
+opMsgOpcode :: Opcode
+opMsgOpcode = 2013
+
 putRequest :: Request -> RequestId -> Put
 putRequest request requestId = do
     putHeader (qOpcode request) requestId
@@ -418,6 +705,10 @@
             putCString gFullCollection
             putInt32 gBatchSize
             putInt64 gCursorId
+        Message{..} -> do
+            putInt32 0
+            putInt8 0
+            putDocument $ merge [ "$db" =: mDatabase ] mParams
 
 qBit :: QueryOption -> Int32
 qBit TailableCursor = bit 1
@@ -425,7 +716,7 @@
 qBit NoCursorTimeout = bit 4
 qBit AwaitData = bit 5
 --qBit Exhaust = bit 6
-qBit Partial = bit 7
+qBit Database.MongoDB.Internal.Protocol.Partial = bit 7
 
 qBits :: [QueryOption] -> Int32
 qBits = bitOr . map qBit
@@ -438,7 +729,13 @@
     rCursorId :: CursorId,  -- ^ 0 = cursor finished
     rStartingFrom :: Int32,
     rDocuments :: [Document]
-    } deriving (Show, Eq)
+    }
+   | ReplyOpMsg {
+        flagBits :: [FlagBit],
+        sections :: [Document],
+        checksum :: Maybe Int32
+    }
+    deriving (Show, Eq)
 
 data ResponseFlag =
       CursorNotFound  -- ^ Set when getMore is called but the cursor id is not valid at the server. Returned with zero results.
@@ -454,16 +751,37 @@
 getReply :: Get (ResponseTo, Reply)
 getReply = do
     (opcode, responseTo) <- getHeader
-    unless (opcode == replyOpcode) $ fail $ "expected reply opcode (1) but got " ++ show opcode
-    rResponseFlags <-  rFlags <$> getInt32
-    rCursorId <- getInt64
-    rStartingFrom <- getInt32
-    numDocs <- fromIntegral <$> getInt32
-    rDocuments <- replicateM numDocs getDocument
-    return (responseTo, Reply{..})
+    if opcode == 2013
+      then do
+            -- Notes:
+            -- Checksum bits that are set by the server don't seem to be supported by official drivers.
+            -- See: https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/message.py#L1423
+            flagBits <-  rFlagsOpMsg <$> getInt32
+            _ <- getInt8
+            sec0 <- getDocument
+            let sections = [sec0]
+                checksum = Nothing
+            return (responseTo, ReplyOpMsg{..})
+      else do
+          unless (opcode == replyOpcode) $ fail $ "expected reply opcode (1) but got " ++ show opcode
+          rResponseFlags <-  rFlags <$> getInt32
+          rCursorId <- getInt64
+          rStartingFrom <- getInt32
+          numDocs <- fromIntegral <$> getInt32
+          rDocuments <- replicateM numDocs getDocument
+          return (responseTo, Reply{..})
 
 rFlags :: Int32 -> [ResponseFlag]
 rFlags bits = filter (testBit bits . rBit) [CursorNotFound ..]
+
+-- See https://github.com/mongodb/specifications/blob/master/source/message/OP_MSG.rst#flagbits
+rFlagsOpMsg :: Int32 -> [FlagBit]
+rFlagsOpMsg bits = isValidFlag bits
+  where isValidFlag bt =
+          let setBits = map fst $ filter (\(_,b) -> b == True) $ zip ([0..31] :: [Int32]) $ map (testBit bt) [0 .. 31]
+          in if any (\n -> not $ elem n [0,1,16]) setBits
+               then error "Unsopported bit was set"
+               else filter (testBit bt . bitOpMsg) [ChecksumPresent ..]
 
 rBit :: ResponseFlag -> Int
 rBit CursorNotFound = 0
diff --git a/Database/MongoDB/Internal/Util.hs b/Database/MongoDB/Internal/Util.hs
--- a/Database/MongoDB/Internal/Util.hs
+++ b/Database/MongoDB/Internal/Util.hs
@@ -1,9 +1,7 @@
--- | Miscellaneous general functions and Show, Eq, and Ord instances for PortID
+-- | Miscellaneous general functions
 
 {-# LANGUAGE FlexibleInstances, UndecidableInstances, StandaloneDeriving #-}
 {-# LANGUAGE CPP #-}
--- PortID instances
-{-# OPTIONS_GHC -fno-warn-orphans #-}
 
 module Database.MongoDB.Internal.Util where
 
@@ -14,26 +12,19 @@
 import Control.Monad (liftM, liftM2)
 import Data.Bits (Bits, (.|.))
 import Data.Word (Word8)
-import Network (PortID(..))
 import Numeric (showHex)
 import System.Random (newStdGen)
 import System.Random.Shuffle (shuffle')
 
 import qualified Data.ByteString as S
 
-import Control.Monad.Error (MonadError(..), Error(..))
+import Control.Monad.Except (MonadError(..))
 import Control.Monad.Trans (MonadIO, liftIO)
 import Data.Bson
 import Data.Text (Text)
 
 import qualified Data.Text as T
 
-#if !MIN_VERSION_network(2, 4, 1)
-deriving instance Show PortID
-deriving instance Eq PortID
-#endif
-deriving instance Ord PortID
-
 -- | A monadic sort implementation derived from the non-monadic one in ghc's Prelude
 mergesortM :: Monad m => (a -> a -> m Ordering) -> [a] -> m [a]
 mergesortM cmp = mergesortM' cmp . map wrap
@@ -69,9 +60,12 @@
 -- ^ Repeatedy execute action, collecting results, until it returns Nothing
 loop act = act >>= maybe (return []) (\a -> (a :) `liftM` loop act)
 
-untilSuccess :: (MonadError e m, Error e) => (a -> m b) -> [a] -> m b
+untilSuccess :: (MonadError e m) => (a -> m b) -> [a] -> m b
 -- ^ Apply action to elements one at a time until one succeeds. Throw last error if all fail. Throw 'strMsg' error if list is empty.
-untilSuccess = untilSuccess' (strMsg "empty untilSuccess")
+untilSuccess = untilSuccess' (error "empty untilSuccess")
+-- Use 'error' copying behavior in removed 'Control.Monad.Error.Error' instance:
+-- instance Error Failure where strMsg = error
+-- 'fail' is treated the same as a programming 'error'. In other words, don't use it.
 
 untilSuccess' :: (MonadError e m) => e -> (a -> m b) -> [a] -> m b
 -- ^ Apply action to elements one at a time until one succeeds. Throw last error if all fail. Throw given error if list is empty
@@ -97,6 +91,9 @@
 (<.>) :: Text -> Text -> Text
 -- ^ Concat first and second together with period in between. Eg. @\"hello\" \<.\> \"world\" = \"hello.world\"@
 a <.> b = T.append a (T.cons '.' b)
+
+splitDot :: Text -> (Text, Text)
+splitDot t = let (pre, post) = T.break (== '.') t in (pre, T.drop 1 post) 
 
 true1 :: Label -> Document -> Bool
 -- ^ Is field's value a 1 or True (MongoDB use both Int and Bools for truth values). Error if field not in document or field not a Num or Bool.
diff --git a/Database/MongoDB/Query.hs b/Database/MongoDB/Query.hs
--- a/Database/MongoDB/Query.hs
+++ b/Database/MongoDB/Query.hs
@@ -1,1473 +1,1931 @@
 -- | Query and update documents
 
-{-# LANGUAGE OverloadedStrings, RecordWildCards, NamedFieldPuns, TupleSections, FlexibleContexts, FlexibleInstances, UndecidableInstances, MultiParamTypeClasses, GeneralizedNewtypeDeriving, StandaloneDeriving, TypeSynonymInstances, TypeFamilies, CPP, DeriveDataTypeable, ScopedTypeVariables, BangPatterns #-}
-
-module Database.MongoDB.Query (
-    -- * Monad
-    Action, access, Failure(..), ErrorCode,
-    AccessMode(..), GetLastError, master, slaveOk, accessMode,
-    liftDB,
-    MongoContext(..), HasMongoContext(..),
-    -- * Database
-    Database, allDatabases, useDb, thisDatabase,
-    -- ** Authentication
-    Username, Password, auth, authMongoCR, authSCRAMSHA1,
-    -- * Collection
-    Collection, allCollections,
-    -- ** Selection
-    Selection(..), Selector, whereJS,
-    Select(select),
-    -- * Write
-    -- ** Insert
-    insert, insert_, insertMany, insertMany_, insertAll, insertAll_,
-    -- ** Update
-    save, replace, repsert, upsert, Modifier, modify, updateMany, updateAll,
-    WriteResult(..), UpdateOption(..), Upserted(..),
-    -- ** Delete
-    delete, deleteOne, deleteMany, deleteAll, DeleteOption(..),
-    -- * Read
-    -- ** Query
-    Query(..), QueryOption(NoCursorTimeout, TailableCursor, AwaitData, Partial),
-    Projector, Limit, Order, BatchSize,
-    explain, find, findOne, fetch,
-    findAndModify, findAndModifyOpts, FindAndModifyOpts(..), defFamUpdateOpts,
-    count, distinct,
-    -- *** Cursor
-    Cursor, nextBatch, next, nextN, rest, closeCursor, isCursorClosed,
-    -- ** Aggregate
-    Pipeline, AggregateConfig(..), aggregate, aggregateCursor,
-    -- ** Group
-    Group(..), GroupKey(..), group,
-    -- ** MapReduce
-    MapReduce(..), MapFun, ReduceFun, FinalizeFun, MROut(..), MRMerge(..),
-    MRResult, mapReduce, runMR, runMR',
-    -- * Command
-    Command, runCommand, runCommand1,
-    eval, retrieveServerData, ServerData(..)
-) where
-
-import Prelude hiding (lookup)
-import Control.Exception (Exception, throwIO)
-import Control.Monad (unless, replicateM, liftM, liftM2)
-import Data.Default.Class (Default(..))
-import Data.Int (Int32, Int64)
-import Data.Either (lefts, rights)
-import Data.List (foldl1')
-import Data.Maybe (listToMaybe, catMaybes, isNothing)
-import Data.Word (Word32)
-#if !MIN_VERSION_base(4,8,0)
-import Data.Monoid (mappend)
-#endif
-import Data.Typeable (Typeable)
-import System.Mem.Weak (Weak)
-
-import qualified Control.Concurrent.MVar as MV
-#if MIN_VERSION_base(4,6,0)
-import Control.Concurrent.MVar.Lifted (MVar,
-                                       readMVar)
-#else
-import Control.Concurrent.MVar.Lifted (MVar, addMVarFinalizer,
-                                         readMVar)
-#endif
-import Control.Applicative ((<$>))
-import Control.Exception (catch)
-import Control.Monad (when, void)
-import Control.Monad.Error (Error(..))
-import Control.Monad.Reader (MonadReader, ReaderT, runReaderT, ask, asks, local)
-import Control.Monad.Trans (MonadIO, liftIO)
-import Data.Binary.Put (runPut)
-import Data.Bson (Document, Field(..), Label, Val, Value(String, Doc, Bool),
-                  Javascript, at, valueAt, lookup, look, genObjectId, (=:),
-                  (=?), (!?), Val(..), ObjectId, Value(..))
-import Data.Bson.Binary (putDocument)
-import Data.Text (Text)
-import qualified Data.Text as T
-
-import Database.MongoDB.Internal.Protocol (Reply(..), QueryOption(..),
-                                           ResponseFlag(..), InsertOption(..),
-                                           UpdateOption(..), DeleteOption(..),
-                                           CursorId, FullCollection, Username,
-                                           Password, Pipe, Notice(..),
-                                           Request(GetMore, qOptions, qSkip,
-                                           qFullCollection, qBatchSize,
-                                           qSelector, qProjector),
-                                           pwKey, ServerData(..))
-import Database.MongoDB.Internal.Util (loop, liftIOE, true1, (<.>))
-import qualified Database.MongoDB.Internal.Protocol as P
-
-import qualified Crypto.Nonce as Nonce
-import qualified Data.ByteString as BS
-import qualified Data.ByteString.Lazy as LBS
-import qualified Data.ByteString.Base16 as B16
-import qualified Data.ByteString.Base64 as B64
-import qualified Data.ByteString.Char8 as B
-import qualified Data.Either as E
-import qualified Crypto.Hash.MD5 as MD5
-import qualified Crypto.Hash.SHA1 as SHA1
-import qualified Crypto.MAC.HMAC as HMAC
-import Data.Bits (xor)
-import qualified Data.Map as Map
-import Text.Read (readMaybe)
-import Data.Maybe (fromMaybe)
-
--- * Monad
-
-type Action = ReaderT MongoContext
--- ^ A monad on top of m (which must be a MonadIO) that may access the database and may fail with a DB 'Failure'
-
-access :: (MonadIO m) => Pipe -> AccessMode -> Database -> Action m a -> m a
--- ^ Run action against database on server at other end of pipe. Use access mode for any reads and writes.
--- Throw 'Failure' in case of any error.
-access mongoPipe mongoAccessMode mongoDatabase action = runReaderT action MongoContext{..}
-
--- | A connection failure, or a read or write exception like cursor expired or inserting a duplicate key.
--- Note, unexpected data from the server is not a Failure, rather it is a programming error (you should call 'error' in this case) because the client and server are incompatible and requires a programming change.
-data Failure =
-     ConnectionFailure IOError  -- ^ TCP connection ('Pipeline') failed. May work if you try again on the same Mongo 'Connection' which will create a new Pipe.
-    | CursorNotFoundFailure CursorId  -- ^ Cursor expired because it wasn't accessed for over 10 minutes, or this cursor came from a different server that the one you are currently connected to (perhaps a fail over happen between servers in a replica set)
-    | QueryFailure ErrorCode String  -- ^ Query failed for some reason as described in the string
-    | WriteFailure Int ErrorCode String -- ^ Error observed by getLastError after a write, error description is in string, index of failed document is the first argument
-    | WriteConcernFailure Int String  -- ^ Write concern error. It's reported only by insert, update, delete commands. Not by wire protocol.
-    | DocNotFound Selection  -- ^ 'fetch' found no document matching selection
-    | AggregateFailure String -- ^ 'aggregate' returned an error
-    | CompoundFailure [Failure] -- ^ When we need to aggregate several failures and report them.
-    | ProtocolFailure Int String -- ^ The structure of the returned documents doesn't match what we expected
-    deriving (Show, Eq, Typeable)
-instance Exception Failure
-
-type ErrorCode = Int
--- ^ Error code from getLastError or query failure
-
-instance Error Failure where strMsg = error
--- ^ 'fail' is treated the same as a programming 'error'. In other words, don't use it.
-
--- | Type of reads and writes to perform
-data AccessMode =
-     ReadStaleOk  -- ^ Read-only action, reading stale data from a slave is OK.
-    | UnconfirmedWrites  -- ^ Read-write action, slave not OK, every write is fire & forget.
-    | ConfirmWrites GetLastError  -- ^ Read-write action, slave not OK, every write is confirmed with getLastError.
-    deriving Show
-
-type GetLastError = Document
--- ^ Parameters for getLastError command. For example @[\"w\" =: 2]@ tells the server to wait for the write to reach at least two servers in replica set before acknowledging. See <http://www.mongodb.org/display/DOCS/Last+Error+Commands> for more options.
-
-class Result a where
-  isFailed :: a -> Bool
-
-data WriteResult = WriteResult
-                  { failed      :: Bool
-                  , nMatched    :: Int
-                  , nModified   :: Maybe Int
-                  , nRemoved    :: Int
-                  -- ^ Mongodb server before 2.6 doesn't allow to calculate this value.
-                  -- This field is meaningless if we can't calculate the number of modified documents.
-                  , upserted    :: [Upserted]
-                  , writeErrors :: [Failure]
-                  , writeConcernErrors :: [Failure]
-                  } deriving Show
-
-instance Result WriteResult where
-  isFailed = failed
-
-instance Result (Either a b) where
-  isFailed (Left _) = True
-  isFailed _ = False
-
-data Upserted = Upserted
-              { upsertedIndex :: Int
-              , upsertedId    :: ObjectId
-              } deriving Show
-
-master :: AccessMode
--- ^ Same as 'ConfirmWrites' []
-master = ConfirmWrites []
-
-slaveOk :: AccessMode
--- ^ Same as 'ReadStaleOk'
-slaveOk = ReadStaleOk
-
-accessMode :: (Monad m) => AccessMode -> Action m a -> Action m a
--- ^ Run action with given 'AccessMode'
-accessMode mode act = local (\ctx -> ctx {mongoAccessMode = mode}) act
-
-readMode :: AccessMode -> ReadMode
-readMode ReadStaleOk = StaleOk
-readMode _ = Fresh
-
-writeMode :: AccessMode -> WriteMode
-writeMode ReadStaleOk = Confirm []
-writeMode UnconfirmedWrites = NoConfirm
-writeMode (ConfirmWrites z) = Confirm z
-
--- | Values needed when executing a db operation
-data MongoContext = MongoContext {
-    mongoPipe :: Pipe, -- ^ operations read/write to this pipelined TCP connection to a MongoDB server
-    mongoAccessMode :: AccessMode, -- ^ read/write operation will use this access mode
-    mongoDatabase :: Database } -- ^ operations query/update this database
-
-mongoReadMode :: MongoContext -> ReadMode
-mongoReadMode = readMode . mongoAccessMode
-
-mongoWriteMode :: MongoContext -> WriteMode
-mongoWriteMode = writeMode . mongoAccessMode
-
-class HasMongoContext env where
-    mongoContext :: env -> MongoContext
-instance HasMongoContext MongoContext where
-    mongoContext = id
-
-liftDB :: (MonadReader env m, HasMongoContext env, MonadIO m)
-       => Action IO a
-       -> m a
-liftDB m = do
-    env <- ask
-    liftIO $ runReaderT m (mongoContext env)
-
--- * Database
-
-type Database = Text
-
-allDatabases :: (MonadIO m) => Action m [Database]
--- ^ List all databases residing on server
-allDatabases = (map (at "name") . at "databases") `liftM` useDb "admin" (runCommand1 "listDatabases")
-
-thisDatabase :: (Monad m) => Action m Database
--- ^ Current database in use
-thisDatabase = asks mongoDatabase
-
-useDb :: (Monad m) => Database -> Action m a -> Action m a
--- ^ Run action against given database
-useDb db act = local (\ctx -> ctx {mongoDatabase = db}) act
-
--- * Authentication
-
-auth :: MonadIO m => Username -> Password -> Action m Bool
--- ^ Authenticate with the current database (if server is running in secure mode). Return whether authentication was successful or not. Reauthentication is required for every new pipe. SCRAM-SHA-1 will be used for server versions 3.0+, MONGO-CR for lower versions.
-auth un pw = do
-    let serverVersion = liftM (at "version") $ useDb "admin" $ runCommand ["buildinfo" =: (1 :: Int)]
-    mmv <- liftM (readMaybe . T.unpack . head . T.splitOn ".") $ serverVersion
-    maybe (return False) performAuth mmv
-    where
-    performAuth majorVersion =
-        case (majorVersion >= (3 :: Int)) of
-            True -> authSCRAMSHA1 un pw
-            False -> authMongoCR un pw
-
-authMongoCR :: (MonadIO m) => Username -> Password -> Action m Bool
--- ^ Authenticate with the current database, using the MongoDB-CR authentication mechanism (default in MongoDB server < 3.0)
-authMongoCR usr pss = do
-    n <- at "nonce" `liftM` runCommand ["getnonce" =: (1 :: Int)]
-    true1 "ok" `liftM` runCommand ["authenticate" =: (1 :: Int), "user" =: usr, "nonce" =: n, "key" =: pwKey n usr pss]
-
-authSCRAMSHA1 :: MonadIO m => Username -> Password -> Action m Bool
--- ^ Authenticate with the current database, using the SCRAM-SHA-1 authentication mechanism (default in MongoDB server >= 3.0)
-authSCRAMSHA1 un pw = do
-    let hmac = HMAC.hmac SHA1.hash 64
-    nonce <- liftIO (Nonce.withGenerator Nonce.nonce128 >>= return . B64.encode)
-    let firstBare = B.concat [B.pack $ "n=" ++ (T.unpack un) ++ ",r=", nonce]
-    let client1 = ["saslStart" =: (1 :: Int), "mechanism" =: ("SCRAM-SHA-1" :: String), "payload" =: (B.unpack . B64.encode $ B.concat [B.pack "n,,", firstBare]), "autoAuthorize" =: (1 :: Int)]
-    server1 <- runCommand client1
-
-    shortcircuit (true1 "ok" server1) $ do
-        let serverPayload1 = B64.decodeLenient . B.pack . at "payload" $ server1
-        let serverData1 = parseSCRAM serverPayload1
-        let iterations = read . B.unpack $ Map.findWithDefault "1" "i" serverData1
-        let salt = B64.decodeLenient $ Map.findWithDefault "" "s" serverData1
-        let snonce = Map.findWithDefault "" "r" serverData1
-
-        shortcircuit (B.isInfixOf nonce snonce) $ do
-            let withoutProof = B.concat [B.pack "c=biws,r=", snonce]
-            let digestS = B.pack $ T.unpack un ++ ":mongo:" ++ T.unpack pw
-            let digest = B16.encode $ MD5.hash digestS
-            let saltedPass = scramHI digest salt iterations
-            let clientKey = hmac saltedPass (B.pack "Client Key")
-            let storedKey = SHA1.hash clientKey
-            let authMsg = B.concat [firstBare, B.pack ",", serverPayload1, B.pack ",", withoutProof]
-            let clientSig = hmac storedKey authMsg
-            let pval = B64.encode . BS.pack $ BS.zipWith xor clientKey clientSig
-            let clientFinal = B.concat [withoutProof, B.pack ",p=", pval]
-            let serverKey = hmac saltedPass (B.pack "Server Key")
-            let serverSig = B64.encode $ hmac serverKey authMsg
-            let client2 = ["saslContinue" =: (1 :: Int), "conversationId" =: (at "conversationId" server1 :: Int), "payload" =: (B.unpack $ B64.encode clientFinal)]
-            server2 <- runCommand client2
-
-            shortcircuit (true1 "ok" server2) $ do
-                let serverPayload2 = B64.decodeLenient . B.pack $ at "payload" server2
-                let serverData2 = parseSCRAM serverPayload2
-                let serverSigComp = Map.findWithDefault "" "v" serverData2
-
-                shortcircuit (serverSig == serverSigComp) $ do
-                  let done = true1 "done" server2
-                  if done
-                    then return True
-                    else do
-                      let client2Step2 = [ "saslContinue" =: (1 :: Int)
-                                         , "conversationId" =: (at "conversationId" server1 :: Int)
-                                         , "payload" =: String ""]
-                      server3 <- runCommand client2Step2
-                      shortcircuit (true1 "ok" server3) $ do
-                        return True
-    where
-    shortcircuit True f = f
-    shortcircuit False _ = return False
-
-scramHI :: B.ByteString -> B.ByteString -> Int -> B.ByteString
-scramHI digest salt iters = snd $ foldl com (u1, u1) [1..(iters-1)]
-    where
-    hmacd = HMAC.hmac SHA1.hash 64 digest
-    u1 = hmacd (B.concat [salt, BS.pack [0, 0, 0, 1]])
-    com (u,uc) _ = let u' = hmacd u in (u', BS.pack $ BS.zipWith xor uc u')
-
-parseSCRAM :: B.ByteString -> Map.Map B.ByteString B.ByteString
-parseSCRAM = Map.fromList . fmap cleanup . (fmap $ T.breakOn "=") . T.splitOn "," . T.pack . B.unpack
-    where cleanup (t1, t2) = (B.pack $ T.unpack t1, B.pack . T.unpack $ T.drop 1 t2)
-
-retrieveServerData :: (MonadIO m) => Action m ServerData
-retrieveServerData = do
-  d <- runCommand1 "isMaster"
-  let newSd = ServerData
-                { isMaster = (fromMaybe False $ lookup "ismaster" d)
-                , minWireVersion = (fromMaybe 0 $ lookup "minWireVersion" d)
-                , maxWireVersion = (fromMaybe 0 $ lookup "maxWireVersion" d)
-                , maxMessageSizeBytes = (fromMaybe 48000000 $ lookup "maxMessageSizeBytes" d)
-                , maxBsonObjectSize = (fromMaybe (16 * 1024 * 1024) $ lookup "maxBsonObjectSize" d)
-                , maxWriteBatchSize = (fromMaybe 1000 $ lookup "maxWriteBatchSize" d)
-                }
-  return newSd
-
--- * Collection
-
-type Collection = Text
--- ^ Collection name (not prefixed with database)
-
-allCollections :: MonadIO m => Action m [Collection]
--- ^ List all collections in this database
-allCollections = do
-    p <- asks mongoPipe
-    let sd = P.serverData p
-    if (maxWireVersion sd <= 2)
-      then do
-        db <- thisDatabase
-        docs <- rest =<< find (query [] "system.namespaces") {sort = ["name" =: (1 :: Int)]}
-        return . filter (not . isSpecial db) . map dropDbPrefix $ map (at "name") docs
-      else do
-        r <- runCommand1 "listCollections"
-        let curData = do
-                   (Doc curDoc) <- r !? "cursor"
-                   (curId :: Int64) <- curDoc !? "id"
-                   (curNs :: Text) <- curDoc !? "ns"
-                   (firstBatch :: [Value]) <- curDoc !? "firstBatch"
-                   return $ (curId, curNs, ((catMaybes (map cast' firstBatch)) :: [Document]))
-        case curData of
-          Nothing -> return []
-          Just (curId, curNs, firstBatch) -> do
-            db <- thisDatabase
-            nc <- newCursor db curNs 0 $ return $ Batch Nothing curId firstBatch
-            docs <- rest nc
-            return $ catMaybes $ map (\d -> (d !? "name")) docs
- where
-    dropDbPrefix = T.tail . T.dropWhile (/= '.')
-    isSpecial db col = T.any (== '$') col && db <.> col /= "local.oplog.$main"
-
--- * Selection
-
-data Selection = Select {selector :: Selector, coll :: Collection}  deriving (Show, Eq)
--- ^ Selects documents in collection that match selector
-
-type Selector = 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
-whereJS sel js = ("$where" =: js) : sel
-
-class Select aQueryOrSelection where
-    select :: Selector -> Collection -> aQueryOrSelection
-    -- ^ 'Query' or 'Selection' that selects documents in collection that match selector. The choice of type depends on use, for example, in @'find' (select sel col)@ it is a Query, and in @'delete' (select sel col)@ it is a Selection.
-
-instance Select Selection where
-    select = Select
-
-instance Select Query where
-    select = query
-
--- * Write
-
-data WriteMode =
-      NoConfirm  -- ^ Submit writes without receiving acknowledgments. Fast. Assumes writes succeed even though they may not.
-    | Confirm GetLastError  -- ^ Receive an acknowledgment after every write, and raise exception if one says the write failed. This is acomplished by sending the getLastError command, with given 'GetLastError' parameters, after every write.
-    deriving (Show, Eq)
-
-write :: Notice -> Action IO (Maybe Document)
--- ^ Send write to server, and if write-mode is 'Safe' then include getLastError request and raise 'WriteFailure' if it reports an error.
-write notice = asks mongoWriteMode >>= \mode -> case mode of
-    NoConfirm -> do
-      pipe <- asks mongoPipe
-      liftIOE ConnectionFailure $ P.send pipe [notice]
-      return Nothing
-    Confirm params -> do
-        let q = query (("getlasterror" =: (1 :: Int)) : params) "$cmd"
-        pipe <- asks mongoPipe
-        Batch _ _ [doc] <- do
-          r <- queryRequest False q {limit = 1}
-          rr <- liftIO $ request pipe [notice] r
-          fulfill rr
-        return $ Just doc
-
--- ** Insert
-
-insert :: (MonadIO m) => Collection -> Document -> Action m Value
--- ^ Insert document into collection and return its \"_id\" value, which is created automatically if not supplied
-insert col doc = do
-  doc' <- liftIO $ assignId doc
-  res <- insertBlock [] col (0, [doc'])
-  case res of
-    Left failure -> liftIO $ throwIO failure
-    Right r -> return $ head r
-
-insert_ :: (MonadIO m) => Collection -> Document -> Action m ()
--- ^ Same as 'insert' except don't return _id
-insert_ col doc = insert col doc >> return ()
-
-insertMany :: (MonadIO m) => Collection -> [Document] -> Action m [Value]
--- ^ Insert documents into collection and return their \"_id\" values,
--- which are created automatically if not supplied.
--- If a document fails to be inserted (eg. due to duplicate key)
--- then remaining docs are aborted, and LastError is set.
--- An exception will be throw if any error occurs.
-insertMany = insert' []
-
-insertMany_ :: (MonadIO m) => Collection -> [Document] -> Action m ()
--- ^ Same as 'insertMany' except don't return _ids
-insertMany_ col docs = insertMany col docs >> return ()
-
-insertAll :: (MonadIO m) => Collection -> [Document] -> Action m [Value]
--- ^ Insert documents into collection and return their \"_id\" values,
--- which are created automatically if not supplied. If a document fails
--- to be inserted (eg. due to duplicate key) then remaining docs
--- are still inserted.
-insertAll = insert' [KeepGoing]
-
-insertAll_ :: (MonadIO m) => Collection -> [Document] -> Action m ()
--- ^ Same as 'insertAll' except don't return _ids
-insertAll_ col docs = insertAll col docs >> return ()
-
-insertCommandDocument :: [InsertOption] -> Collection -> [Document] -> Document -> Document
-insertCommandDocument opts col docs writeConcern =
-          [ "insert" =: col
-          , "ordered" =: (KeepGoing `notElem` opts)
-          , "documents" =: docs
-          , "writeConcern" =: writeConcern
-          ]
-
-takeRightsUpToLeft :: [Either a b] -> [b]
-takeRightsUpToLeft l = E.rights $ takeWhile E.isRight l
-
-insert' :: (MonadIO m)
-        => [InsertOption] -> Collection -> [Document] -> Action m [Value]
--- ^ Insert documents into collection and return their \"_id\" values, which are created automatically if not supplied
-insert' opts col docs = do
-  p <- asks mongoPipe
-  let sd = P.serverData p
-  docs' <- liftIO $ mapM assignId docs
-  mode <- asks mongoWriteMode
-  let writeConcern = case mode of
-                        NoConfirm -> ["w" =: (0 :: Int)]
-                        Confirm params -> params
-  let docSize = sizeOfDocument $ insertCommandDocument opts col [] writeConcern
-  let ordered = (not (KeepGoing `elem` opts))
-  let preChunks = splitAtLimit
-                      (maxBsonObjectSize sd - docSize)
-                                           -- size of auxiliary part of insert
-                                           -- document should be subtracted from
-                                           -- the overall size
-                      (maxWriteBatchSize sd)
-                      docs'
-  let chunks =
-        if ordered
-            then takeRightsUpToLeft preChunks
-            else rights preChunks
-
-  let lens = map length chunks
-  let lSums = 0 : (zipWith (+) lSums lens)
-
-  chunkResults <- interruptibleFor ordered (zip lSums chunks) $ insertBlock opts col
-
-  let lchunks = lefts preChunks
-  when (not $ null lchunks) $ do
-    liftIO $ throwIO $ head lchunks
-
-  let lresults = lefts chunkResults
-  when (not $ null lresults) $ liftIO $ throwIO $ head lresults
-  return $ concat $ rights chunkResults
-
-insertBlock :: (MonadIO m)
-            => [InsertOption] -> Collection -> (Int, [Document]) -> Action m (Either Failure [Value])
--- ^ This will fail if the list of documents is bigger than restrictions
-insertBlock _ _ (_, []) = return $ Right []
-insertBlock opts col (prevCount, docs) = do
-    db <- thisDatabase
-
-    p <- asks mongoPipe
-    let sd = P.serverData p
-    if (maxWireVersion sd < 2)
-      then do
-        res <- liftDB $ write (Insert (db <.> col) opts docs)
-        let errorMessage = do
-              jRes <- res
-              em <- lookup "err" jRes
-              return $ WriteFailure prevCount (maybe 0 id $ lookup "code" jRes)  em
-              -- In older versions of ^^ the protocol we can't really say which document failed.
-              -- So we just report the accumulated number of documents in the previous blocks.
-
-        case errorMessage of
-          Just failure -> return $ Left failure
-          Nothing -> return $ Right $ map (valueAt "_id") docs
-      else do
-        mode <- asks mongoWriteMode
-        let writeConcern = case mode of
-                              NoConfirm -> ["w" =: (0 :: Int)]
-                              Confirm params -> params
-        doc <- runCommand $ insertCommandDocument opts col docs writeConcern
-        case (look "writeErrors" doc, look "writeConcernError" doc) of
-          (Nothing, Nothing) -> return $ Right $ map (valueAt "_id") docs
-          (Just (Array errs), Nothing) -> do
-            let writeErrors = map (anyToWriteError prevCount) $ errs
-            let errorsWithFailureIndex = map (addFailureIndex prevCount) writeErrors
-            return $ Left $ CompoundFailure errorsWithFailureIndex
-          (Nothing, Just err) -> do
-            return $ Left $ WriteFailure
-                                    prevCount
-                                    (maybe 0 id $ lookup "ok" doc)
-                                    (show err)
-          (Just (Array errs), Just writeConcernErr) -> do
-            let writeErrors = map (anyToWriteError prevCount) $ errs
-            let errorsWithFailureIndex = map (addFailureIndex prevCount) writeErrors
-            return $ Left $ CompoundFailure $ (WriteFailure
-                                    prevCount
-                                    (maybe 0 id $ lookup "ok" doc)
-                                    (show writeConcernErr)) : errorsWithFailureIndex
-          (Just unknownValue, Nothing) -> do
-            return $ Left $ ProtocolFailure prevCount $ "Expected array of errors. Received: " ++ show unknownValue
-          (Just unknownValue, Just writeConcernErr) -> do
-            return $ Left $ CompoundFailure $ [ ProtocolFailure prevCount $ "Expected array of errors. Received: " ++ show unknownValue
-                                              , WriteFailure prevCount (maybe 0 id $ lookup "ok" doc) $ show writeConcernErr]
-
-splitAtLimit :: Int -> Int -> [Document] -> [Either Failure [Document]]
-splitAtLimit maxSize maxCount list = chop (go 0 0 []) list
-  where
-    go :: Int -> Int -> [Document] -> [Document] -> ((Either Failure [Document]), [Document])
-    go _ _ res [] = (Right $ reverse res, [])
-    go curSize curCount [] (x:xs) |
-      ((curSize + (sizeOfDocument x) + 2 + curCount) > maxSize) =
-        (Left $ WriteFailure 0 0 "One document is too big for the message", xs)
-    go curSize curCount res (x:xs) =
-      if (   ((curSize + (sizeOfDocument x) + 2 + curCount) > maxSize)
-                                 -- we have ^ 2 brackets and curCount commas in
-                                 -- the document that we need to take into
-                                 -- account
-          || ((curCount + 1) > maxCount))
-        then
-          (Right $ reverse res, x:xs)
-        else
-          go (curSize + (sizeOfDocument x)) (curCount + 1) (x:res) xs
-
-    chop :: ([a] -> (b, [a])) -> [a] -> [b]
-    chop _ [] = []
-    chop f as = let (b, as') = f as in b : chop f as'
-
-sizeOfDocument :: Document -> Int
-sizeOfDocument d = fromIntegral $ LBS.length $ runPut $ putDocument d
-
-assignId :: Document -> IO Document
--- ^ Assign a unique value to _id field if missing
-assignId doc = if any (("_id" ==) . label) doc
-    then return doc
-    else (\oid -> ("_id" =: oid) : doc) `liftM` genObjectId
-
--- ** Update
-
-save :: (MonadIO m)
-     => Collection -> Document -> Action m ()
--- ^ Save document to collection, meaning insert it if its new (has no \"_id\" field) or upsert it if its not new (has \"_id\" field)
-save col doc = case look "_id" doc of
-    Nothing -> insert_ col doc
-    Just i -> upsert (Select ["_id" := i] col) doc
-
-replace :: (MonadIO m)
-        => Selection -> Document -> Action m ()
--- ^ Replace first document in selection with given document
-replace = update []
-
-repsert :: (MonadIO m)
-        => Selection -> Document -> Action m ()
--- ^ Replace first document in selection with given document, or insert document if selection is empty
-repsert = update [Upsert]
-{-# DEPRECATED repsert "use upsert instead" #-}
-
-upsert :: (MonadIO m)
-       => Selection -> Document -> Action m ()
--- ^ Update first document in selection with given document, or insert document if selection is empty
-upsert = update [Upsert]
-
-type Modifier = Document
--- ^ Update operations on fields in a document. See <http://www.mongodb.org/display/DOCS/Updating#Updating-ModifierOperations>
-
-modify :: (MonadIO m)
-       => Selection -> Modifier -> Action m ()
--- ^ Update all documents in selection using given modifier
-modify = update [MultiUpdate]
-
-update :: (MonadIO m)
-       => [UpdateOption] -> Selection -> Document -> Action m ()
--- ^ 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.
-update opts (Select sel col) up = do
-    db <- thisDatabase
-    ctx <- ask
-    liftIO $ runReaderT (void $ write (Update (db <.> col) opts sel up)) ctx
-
-updateCommandDocument :: Collection -> Bool -> [Document] -> Document -> Document
-updateCommandDocument col ordered updates writeConcern =
-  [ "update"  =: col
-  , "ordered" =: ordered
-  , "updates" =: updates
-  , "writeConcern" =: writeConcern
-  ]
-
-{-| Bulk update operation. If one update fails it will not update the remaining
- - documents. Current returned value is only a place holder. With mongodb server
- - before 2.6 it will send update requests one by one. In order to receive
- - error messages in versions under 2.6 you need to user confirmed writes.
- - Otherwise even if the errors had place the list of errors will be empty and
- - the result will be success.  After 2.6 it will use bulk update feature in
- - mongodb.
- -}
-updateMany :: (MonadIO m)
-           => Collection
-           -> [(Selector, Document, [UpdateOption])]
-           -> Action m WriteResult
-updateMany = update' True
-
-{-| Bulk update operation. If one update fails it will proceed with the
- - remaining documents. With mongodb server before 2.6 it will send update
- - requests one by one. In order to receive error messages in versions under
- - 2.6 you need to use confirmed writes.  Otherwise even if the errors had
- - place the list of errors will be empty and the result will be success.
- - After 2.6 it will use bulk update feature in mongodb.
- -}
-updateAll :: (MonadIO m)
-           => Collection
-           -> [(Selector, Document, [UpdateOption])]
-           -> Action m WriteResult
-updateAll = update' False
-
-update' :: (MonadIO m)
-        => Bool
-        -> Collection
-        -> [(Selector, Document, [UpdateOption])]
-        -> Action m WriteResult
-update' ordered col updateDocs = do
-  p <- asks mongoPipe
-  let sd = P.serverData p
-  let updates = map (\(s, d, os) -> [ "q" =: s
-                                    , "u" =: d
-                                    , "upsert" =: (Upsert `elem` os)
-                                    , "multi" =: (MultiUpdate `elem` os)])
-                updateDocs
-
-  mode <- asks mongoWriteMode
-  ctx <- ask
-  liftIO $ do
-    let writeConcern = case mode of
-                          NoConfirm -> ["w" =: (0 :: Int)]
-                          Confirm params -> params
-    let docSize = sizeOfDocument $ updateCommandDocument
-                                                      col
-                                                      ordered
-                                                      []
-                                                      writeConcern
-    let preChunks = splitAtLimit
-                        (maxBsonObjectSize sd - docSize)
-                                             -- size of auxiliary part of update
-                                             -- document should be subtracted from
-                                             -- the overall size
-                        (maxWriteBatchSize sd)
-                        updates
-    let chunks =
-          if ordered
-              then takeRightsUpToLeft preChunks
-              else rights preChunks
-    let lens = map length chunks
-    let lSums = 0 : (zipWith (+) lSums lens)
-    blocks <- interruptibleFor ordered (zip lSums chunks) $ \b -> do
-      ur <- runReaderT (updateBlock ordered col b) ctx
-      return ur
-      `catch` \(e :: Failure) -> do
-        return $ WriteResult True 0 Nothing 0 [] [e] []
-    let failedTotal = or $ map failed blocks
-    let updatedTotal = sum $ map nMatched blocks
-    let modifiedTotal =
-          if all isNothing $ map nModified blocks
-            then Nothing
-            else Just $ sum $ catMaybes $ map nModified blocks
-    let totalWriteErrors = concat $ map writeErrors blocks
-    let totalWriteConcernErrors = concat $ map writeConcernErrors blocks
-
-    let upsertedTotal = concat $ map upserted blocks
-    return $ WriteResult
-                  failedTotal
-                  updatedTotal
-                  modifiedTotal
-                  0 -- nRemoved
-                  upsertedTotal
-                  totalWriteErrors
-                  totalWriteConcernErrors
-
-    `catch` \(e :: Failure) -> return $ WriteResult True 0 Nothing 0 [] [e] []
-
-updateBlock :: (MonadIO m)
-            => Bool -> Collection -> (Int, [Document]) -> Action m WriteResult
-updateBlock ordered col (prevCount, docs) = do
-  p <- asks mongoPipe
-  let sd = P.serverData p
-  if (maxWireVersion sd < 2)
-    then liftIO $ ioError $ userError "updateMany doesn't support mongodb older than 2.6"
-    else do
-      mode <- asks mongoWriteMode
-      let writeConcern = case mode of
-                          NoConfirm -> ["w" =: (0 :: Int)]
-                          Confirm params -> params
-      doc <- runCommand $ updateCommandDocument col ordered docs writeConcern
-
-      let n = fromMaybe 0 $ doc !? "n"
-      let writeErrorsResults =
-            case look "writeErrors" doc of
-              Nothing -> WriteResult False 0 (Just 0) 0 [] [] []
-              Just (Array err) -> WriteResult True 0 (Just 0) 0 [] (map (anyToWriteError prevCount) err) []
-              Just unknownErr -> WriteResult
-                                      True
-                                      0
-                                      (Just 0)
-                                      0
-                                      []
-                                      [ ProtocolFailure
-                                            prevCount
-                                          $ "Expected array of error docs, but received: "
-                                              ++ (show unknownErr)]
-                                      []
-
-      let writeConcernResults =
-            case look "writeConcernError" doc of
-              Nothing ->  WriteResult False 0 (Just 0) 0 [] [] []
-              Just (Doc err) -> WriteResult
-                                    True
-                                    0
-                                    (Just 0)
-                                    0
-                                    []
-                                    []
-                                    [ WriteConcernFailure
-                                        (fromMaybe (-1) $ err !? "code")
-                                        (fromMaybe "" $ err !? "errmsg")
-                                    ]
-              Just unknownErr -> WriteResult
-                                      True
-                                      0
-                                      (Just 0)
-                                      0
-                                      []
-                                      []
-                                      [ ProtocolFailure
-                                            prevCount
-                                          $ "Expected doc in writeConcernError, but received: "
-                                              ++ (show unknownErr)]
-
-      let upsertedList = map docToUpserted $ fromMaybe [] (doc !? "upserted")
-      let successResults = WriteResult False n (doc !? "nModified") 0 upsertedList [] []
-      return $ foldl1' mergeWriteResults [writeErrorsResults, writeConcernResults, successResults]
-
-
-interruptibleFor :: (Monad m, Result b) => Bool -> [a] -> (a -> m b) -> m [b]
-interruptibleFor ordered = go []
-  where
-    go !res [] _ = return $ reverse res
-    go !res (x:xs) f = do
-      y <- f x
-      if isFailed y && ordered
-        then return $ reverse (y:res)
-        else go (y:res) xs f
-
-mergeWriteResults :: WriteResult -> WriteResult -> WriteResult
-mergeWriteResults
-  (WriteResult failed1 nMatched1 nModified1 nDeleted1 upserted1 writeErrors1 writeConcernErrors1)
-  (WriteResult failed2 nMatched2 nModified2 nDeleted2 upserted2 writeErrors2 writeConcernErrors2) =
-    (WriteResult
-        (failed1 || failed2)
-        (nMatched1 + nMatched2)
-        ((liftM2 (+)) nModified1 nModified2)
-        (nDeleted1 + nDeleted2)
-        -- This function is used in foldl1' function. The first argument is the accumulator.
-        -- The list in the accumulator is usually longer than the subsequent value which goes in the second argument.
-        -- So, changing the order of list concatenation allows us to keep linear complexity of the
-        -- whole list accumulation process.
-        (upserted2 ++ upserted1)
-        (writeErrors2 ++ writeErrors1)
-        (writeConcernErrors2 ++ writeConcernErrors1)
-        )
-
-
-docToUpserted :: Document -> Upserted
-docToUpserted doc = Upserted ind uid
-  where
-    ind = at "index" doc
-    uid = at "_id"   doc
-
-docToWriteError :: Document -> Failure
-docToWriteError doc = WriteFailure ind code msg
-  where
-    ind  = at "index"  doc
-    code = at "code"   doc
-    msg  = at "errmsg" doc
-
--- ** Delete
-
-delete :: (MonadIO m)
-       => Selection -> Action m ()
--- ^ Delete all documents in selection
-delete = deleteHelper []
-
-deleteOne :: (MonadIO m)
-          => Selection -> Action m ()
--- ^ Delete first document in selection
-deleteOne = deleteHelper [SingleRemove]
-
-deleteHelper :: (MonadIO m)
-             => [DeleteOption] -> Selection -> Action m ()
-deleteHelper opts (Select sel col) = do
-    db <- thisDatabase
-    ctx <- ask
-    liftIO $ runReaderT (void $ write (Delete (db <.> col) opts sel)) ctx
-
-{-| Bulk delete operation. If one delete fails it will not delete the remaining
- - documents. Current returned value is only a place holder. With mongodb server
- - before 2.6 it will send delete requests one by one. After 2.6 it will use
- - bulk delete feature in mongodb.
- -}
-deleteMany :: (MonadIO m)
-           => Collection
-           -> [(Selector, [DeleteOption])]
-           -> Action m WriteResult
-deleteMany = delete' True
-
-{-| Bulk delete operation. If one delete fails it will proceed with the
- - remaining documents. Current returned value is only a place holder. With
- - mongodb server before 2.6 it will send delete requests one by one. After 2.6
- - it will use bulk delete feature in mongodb.
- -}
-deleteAll :: (MonadIO m)
-          => Collection
-          -> [(Selector, [DeleteOption])]
-          -> Action m WriteResult
-deleteAll = delete' False
-
-deleteCommandDocument :: Collection -> Bool -> [Document] -> Document -> Document
-deleteCommandDocument col ordered deletes writeConcern =
-  [ "delete"       =: col
-  , "ordered"      =: ordered
-  , "deletes"      =: deletes
-  , "writeConcern" =: writeConcern
-  ]
-
-delete' :: (MonadIO m)
-        => Bool
-        -> Collection
-        -> [(Selector, [DeleteOption])]
-        -> Action m WriteResult
-delete' ordered col deleteDocs = do
-  p <- asks mongoPipe
-  let sd = P.serverData p
-  let deletes = map (\(s, os) -> [ "q"     =: s
-                                 , "limit" =: if SingleRemove `elem` os
-                                               then (1 :: Int) -- Remove only one matching
-                                               else (0 :: Int) -- Remove all matching
-                                 ])
-                    deleteDocs
-
-  mode <- asks mongoWriteMode
-  let writeConcern = case mode of
-                        NoConfirm -> ["w" =: (0 :: Int)]
-                        Confirm params -> params
-  let docSize = sizeOfDocument $ deleteCommandDocument col ordered [] writeConcern
-  let preChunks = splitAtLimit
-                      (maxBsonObjectSize sd - docSize)
-                                           -- size of auxiliary part of delete
-                                           -- document should be subtracted from
-                                           -- the overall size
-                      (maxWriteBatchSize sd)
-                      deletes
-  let chunks =
-        if ordered
-            then takeRightsUpToLeft preChunks
-            else rights preChunks
-  ctx <- ask
-  let lens = map length chunks
-  let lSums = 0 : (zipWith (+) lSums lens)
-  blockResult <- liftIO $ interruptibleFor ordered (zip lSums chunks) $ \b -> do
-    dr <- runReaderT (deleteBlock ordered col b) ctx
-    return dr
-    `catch` \(e :: Failure) -> do
-      return $ WriteResult True 0 Nothing 0 [] [e] []
-  return $ foldl1' mergeWriteResults blockResult
-
-
-addFailureIndex :: Int -> Failure -> Failure
-addFailureIndex i (WriteFailure ind code s) = WriteFailure (ind + i) code s
-addFailureIndex _ f = f
-
-deleteBlock :: (MonadIO m)
-            => Bool -> Collection -> (Int, [Document]) -> Action m WriteResult
-deleteBlock ordered col (prevCount, docs) = do
-  p <- asks mongoPipe
-  let sd = P.serverData p
-  if (maxWireVersion sd < 2)
-    then liftIO $ ioError $ userError "deleteMany doesn't support mongodb older than 2.6"
-    else do
-      mode <- asks mongoWriteMode
-      let writeConcern = case mode of
-                          NoConfirm -> ["w" =: (0 :: Int)]
-                          Confirm params -> params
-      doc <- runCommand $ deleteCommandDocument col ordered docs writeConcern
-      let n = fromMaybe 0 $ doc !? "n"
-
-      let successResults = WriteResult False 0 Nothing n [] [] []
-      let writeErrorsResults =
-            case look "writeErrors" doc of
-              Nothing ->  WriteResult False 0 Nothing 0 [] [] []
-              Just (Array err) -> WriteResult True 0 Nothing 0 [] (map (anyToWriteError prevCount) err) []
-              Just unknownErr -> WriteResult
-                                      True
-                                      0
-                                      Nothing
-                                      0
-                                      []
-                                      [ ProtocolFailure
-                                            prevCount
-                                          $ "Expected array of error docs, but received: "
-                                              ++ (show unknownErr)]
-                                      []
-      let writeConcernResults =
-            case look "writeConcernError" doc of
-              Nothing ->  WriteResult False 0 Nothing 0 [] [] []
-              Just (Doc err) -> WriteResult
-                                    True
-                                    0
-                                    Nothing
-                                    0
-                                    []
-                                    []
-                                    [ WriteConcernFailure
-                                        (fromMaybe (-1) $ err !? "code")
-                                        (fromMaybe "" $ err !? "errmsg")
-                                    ]
-              Just unknownErr -> WriteResult
-                                      True
-                                      0
-                                      Nothing
-                                      0
-                                      []
-                                      []
-                                      [ ProtocolFailure
-                                            prevCount
-                                          $ "Expected doc in writeConcernError, but received: "
-                                              ++ (show unknownErr)]
-      return $ foldl1' mergeWriteResults [successResults, writeErrorsResults, writeConcernResults]
-
-anyToWriteError :: Int -> Value -> Failure
-anyToWriteError _ (Doc d) = docToWriteError d
-anyToWriteError ind _ = ProtocolFailure ind "Unknown bson value"
-
--- * Read
-
-data ReadMode =
-      Fresh  -- ^ read from master only
-    | StaleOk  -- ^ read from slave ok
-    deriving (Show, Eq)
-
-readModeOption :: ReadMode -> [QueryOption]
-readModeOption Fresh = []
-readModeOption StaleOk = [SlaveOK]
-
--- ** Query
-
--- | Use 'select' to create a basic query with defaults, then modify if desired. For example, @(select sel col) {limit = 10}@
-data Query = Query {
-    options :: [QueryOption],  -- ^ Default = []
-    selection :: Selection,
-    project :: Projector,  -- ^ \[\] = all fields. Default = []
-    skip :: Word32,  -- ^ Number of initial matching documents to skip. Default = 0
-    limit :: Limit, -- ^ Maximum number of documents to return, 0 = no limit. Default = 0
-    sort :: Order,  -- ^ Sort results by this order, [] = no sort. Default = []
-    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. Default = False
-    batchSize :: BatchSize,  -- ^ The number of document to return in each batch response from the server. 0 means use Mongo default. Default = 0
-    hint :: Order  -- ^ Force MongoDB to use this index, [] = no hint. Default = []
-    } deriving (Show, Eq)
-
-type Projector = 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@.
-
-type Limit = Word32
--- ^ Maximum number of documents to return, i.e. cursor will close after iterating over this number of documents. 0 means no limit.
-
-type Order = 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
-
-type BatchSize = 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.
-query sel col = Query [] (Select sel col) [] 0 0 [] False 0 []
-
-find :: MonadIO m => Query -> Action m Cursor
--- ^ Fetch documents satisfying query
-find q@Query{selection, batchSize} = do
-    db <- thisDatabase
-    pipe <- asks mongoPipe
-    qr <- queryRequest False q
-    dBatch <- liftIO $ request pipe [] qr
-    newCursor db (coll selection) batchSize dBatch
-
-findOne :: (MonadIO m) => Query -> Action m (Maybe Document)
--- ^ Fetch first document satisfying query or Nothing if none satisfy it
-findOne q = do
-    pipe <- asks mongoPipe
-    qr <- queryRequest False q {limit = 1}
-    rq <- liftIO $ request pipe [] qr
-    Batch _ _ docs <- liftDB $ fulfill rq
-    return (listToMaybe docs)
-
-fetch :: (MonadIO m) => Query -> Action m Document
--- ^ Same as 'findOne' except throw 'DocNotFound' if none match
-fetch q = findOne q >>= maybe (liftIO $ throwIO $ DocNotFound $ selection q) return
-
-data FindAndModifyOpts = FamRemove Bool
-                       | FamUpdate
-                         { famUpdate :: Document
-                         , famNew :: Bool
-                         , famUpsert :: Bool
-                         }
-                       deriving Show
-
-defFamUpdateOpts :: Document -> FindAndModifyOpts
-defFamUpdateOpts ups = FamUpdate
-                       { famNew = True
-                       , famUpsert = False
-                       , famUpdate = ups
-                       }
-
--- | runs the findAndModify command as an update without an upsert and new set to true.
--- Returns a single updated document (new option is set to true).
---
--- see 'findAndModifyOpts' if you want to use findAndModify in a differnt way
-findAndModify :: MonadIO m
-              => Query
-              -> Document -- ^ updates
-              -> Action m (Either String Document)
-findAndModify q ups = do
-  eres <- findAndModifyOpts q (defFamUpdateOpts ups)
-  return $ case eres of
-    Left l -> Left l
-    Right r -> case r of
-      -- only possible when upsert is True and new is False
-      Nothing  -> Left "findAndModify: impossible null result"
-      Just doc -> Right doc
-
--- | runs the findAndModify command,
--- allows more options than 'findAndModify'
-findAndModifyOpts :: MonadIO m
-                  => Query
-                  ->FindAndModifyOpts
-                  -> Action m (Either String (Maybe Document))
-findAndModifyOpts (Query {
-    selection = Select sel collection
-  , project = project
-  , sort = sort
-  }) famOpts = do
-    result <- runCommand
-       ([ "findAndModify" := String collection
-        , "query"  := Doc sel
-        , "fields" := Doc project
-        , "sort"   := Doc sort
-        ] ++
-            case famOpts of
-              FamRemove shouldRemove -> [ "remove" := Bool shouldRemove ]
-              FamUpdate {..} ->
-                [ "update" := Doc famUpdate
-                , "new"    := Bool famNew    -- return updated document, not original document
-                , "upsert" := Bool famUpsert -- insert if nothing is found
-                ])
-    return $ case lookupErr result of
-        Just e -> leftErr e
-        Nothing -> case lookup "value" result of
-            Left err   -> leftErr $ "no document found: " `mappend` err
-            Right mdoc -> case mdoc of
-                Just doc@(_:_) -> Right (Just doc)
-                Just [] -> case famOpts of
-                    FamUpdate { famUpsert = True, famNew = False } -> Right Nothing
-                    _ -> leftErr $ show result
-                _  -> leftErr $ show result
-  where
-    leftErr err = Left $ "findAndModify " `mappend` show collection
-        `mappend` "\nfrom query: " `mappend` show sel
-        `mappend` "\nerror: " `mappend` err
-
-    -- return Nothing means ok, Just is the error message
-    lookupErr result = case lookup "lastErrorObject" result of
-        Right errObject -> lookup "err" errObject
-        Left err -> Just err
-
-explain :: (MonadIO m) => Query -> Action m Document
--- ^ Return performance stats of query execution
-explain q = do  -- same as findOne but with explain set to true
-    pipe <- asks mongoPipe
-    qr <- queryRequest True q {limit = 1}
-    r <- liftIO $ request pipe [] qr
-    Batch _ _ docs <- liftDB $ fulfill r
-    return $ if null docs then error ("no explain: " ++ show q) else head docs
-
-count :: (MonadIO m) => Query -> Action m Int
--- ^ Fetch number of documents satisfying query (including effect of skip and/or limit if present)
-count Query{selection = Select sel col, skip, limit} = at "n" `liftM` runCommand
-    (["count" =: col, "query" =: sel, "skip" =: (fromIntegral skip :: Int32)]
-        ++ ("limit" =? if limit == 0 then Nothing else Just (fromIntegral limit :: Int32)))
-
-distinct :: (MonadIO m) => Label -> Selection -> Action m [Value]
--- ^ Fetch distinct values of field in selected documents
-distinct k (Select sel col) = at "values" `liftM` runCommand ["distinct" =: col, "key" =: k, "query" =: sel]
-
-queryRequest :: (Monad m) => Bool -> Query -> Action m (Request, Maybe Limit)
--- ^ Translate Query to Protocol.Query. If first arg is true then add special $explain attribute.
-queryRequest isExplain Query{..} = do
-    ctx <- ask
-    return $ queryRequest' (mongoReadMode ctx) (mongoDatabase ctx)
- where
-    queryRequest' rm db = (P.Query{..}, remainingLimit) where
-        qOptions = readModeOption rm ++ options
-        qFullCollection = db <.> coll selection
-        qSkip = fromIntegral skip
-        (qBatchSize, remainingLimit) = batchSizeRemainingLimit batchSize (if limit == 0 then Nothing else Just limit)
-        qProjector = project
-        mOrder = if null sort then Nothing else Just ("$orderby" =: sort)
-        mSnapshot = if snapshot then Just ("$snapshot" =: True) else Nothing
-        mHint = if null hint then Nothing else Just ("$hint" =: hint)
-        mExplain = if isExplain then Just ("$explain" =: True) else Nothing
-        special = catMaybes [mOrder, mSnapshot, mHint, mExplain]
-        qSelector = if null special then s else ("$query" =: s) : special where s = selector selection
-
-batchSizeRemainingLimit :: BatchSize -> (Maybe Limit) -> (Int32, Maybe Limit)
--- ^ Given batchSize and limit return P.qBatchSize and remaining limit
-batchSizeRemainingLimit batchSize mLimit =
-  let remaining =
-        case mLimit of
-          Nothing    -> batchSize
-          Just limit ->
-            if 0 < batchSize && batchSize < limit
-              then batchSize
-              else limit
-  in (fromIntegral remaining, mLimit)
-
-type DelayedBatch = IO Batch
--- ^ A promised batch which may fail
-
-data Batch = Batch (Maybe Limit) CursorId [Document]
--- ^ CursorId = 0 means cursor is finished. Documents is remaining documents to serve in current batch. Limit is number of documents to return. Nothing means no limit.
-
-request :: Pipe -> [Notice] -> (Request, Maybe Limit) -> IO DelayedBatch
--- ^ Send notices and request and return promised batch
-request pipe ns (req, remainingLimit) = do
-    promise <- liftIOE ConnectionFailure $ P.call pipe ns req
-    let protectedPromise = liftIOE ConnectionFailure promise
-    return $ fromReply remainingLimit =<< protectedPromise
-
-fromReply :: Maybe Limit -> Reply -> DelayedBatch
--- ^ Convert Reply to Batch or Failure
-fromReply limit Reply{..} = do
-    mapM_ checkResponseFlag rResponseFlags
-    return (Batch limit rCursorId rDocuments)
- where
-    -- If response flag indicates failure then throw it, otherwise do nothing
-    checkResponseFlag flag = case flag of
-        AwaitCapable -> return ()
-        CursorNotFound -> throwIO $ CursorNotFoundFailure rCursorId
-        QueryError -> throwIO $ QueryFailure (at "code" $ head rDocuments) (at "$err" $ head rDocuments)
-
-fulfill :: DelayedBatch -> Action IO Batch
--- ^ Demand and wait for result, raise failure if exception
-fulfill = liftIO
-
--- *** Cursor
-
-data Cursor = Cursor FullCollection BatchSize (MVar DelayedBatch)
--- ^ 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 'CursorNotFoundFailure'. Note, a cursor is not closed when the pipe is closed, so you can open another pipe to the same server and continue using the cursor.
-
-newCursor :: MonadIO m => Database -> Collection -> BatchSize -> DelayedBatch -> Action m Cursor
--- ^ Create new cursor. If you don't read all results then close it. Cursor will be closed automatically when all results are read from it or when eventually garbage collected.
-newCursor db col batchSize dBatch = do
-    var <- liftIO $ MV.newMVar dBatch
-    let cursor = Cursor (db <.> col) batchSize var
-    _ <- liftDB $ mkWeakMVar var (closeCursor cursor)
-    return cursor
-
-nextBatch :: MonadIO m => Cursor -> Action m [Document]
--- ^ Return next batch of documents in query result, which will be empty if finished.
-nextBatch (Cursor fcol batchSize var) = liftDB $ modifyMVar var $ \dBatch -> do
-    -- Pre-fetch next batch promise from server and return current batch.
-    Batch mLimit cid docs <- liftDB $ fulfill' fcol batchSize dBatch
-    let newLimit = do
-              limit <- mLimit
-              return $ limit - (min limit $ fromIntegral $ length docs)
-    let emptyBatch = return $ Batch (Just 0) 0 []
-    let getNextBatch = nextBatch' fcol batchSize newLimit cid
-    let resultDocs = (maybe id (take . fromIntegral) mLimit) docs
-    case (cid, newLimit) of
-      (0, _)      -> return (emptyBatch, resultDocs)
-      (_, Just 0) -> do
-        pipe <- asks mongoPipe
-        liftIOE ConnectionFailure $ P.send pipe [KillCursors [cid]]
-        return (emptyBatch, resultDocs)
-      (_, _)      -> (, resultDocs) <$> getNextBatch
-
-fulfill' :: FullCollection -> BatchSize -> DelayedBatch -> Action IO Batch
--- Discard pre-fetched batch if empty with nonzero cid.
-fulfill' fcol batchSize dBatch = do
-    b@(Batch limit cid docs) <- fulfill dBatch
-    if cid /= 0 && null docs && (limit > (Just 0))
-        then nextBatch' fcol batchSize limit cid >>= fulfill
-        else return b
-
-nextBatch' :: (MonadIO m) => FullCollection -> BatchSize -> (Maybe Limit) -> CursorId -> Action m DelayedBatch
-nextBatch' fcol batchSize limit cid = do
-    pipe <- asks mongoPipe
-    liftIO $ request pipe [] (GetMore fcol batchSize' cid, remLimit)
-    where (batchSize', remLimit) = batchSizeRemainingLimit batchSize limit
-
-next :: MonadIO m => Cursor -> Action m (Maybe Document)
--- ^ Return next document in query result, or Nothing if finished.
-next (Cursor fcol batchSize var) = liftDB $ modifyMVar var nextState where
-    -- Pre-fetch next batch promise from server when last one in current batch is returned.
-    -- nextState:: DelayedBatch -> Action m (DelayedBatch, Maybe Document)
-    nextState dBatch = do
-        Batch mLimit cid docs <- liftDB $ fulfill' fcol batchSize dBatch
-        if mLimit == (Just 0)
-          then return (return $ Batch (Just 0) 0 [], Nothing)
-          else
-            case docs of
-                doc : docs' -> do
-                    let newLimit = do
-                              limit <- mLimit
-                              return $ limit - 1
-                    dBatch' <- if null docs' && cid /= 0 && ((newLimit > (Just 0)) || (isNothing newLimit))
-                        then nextBatch' fcol batchSize newLimit cid
-                        else return $ return (Batch newLimit cid docs')
-                    when (newLimit == (Just 0)) $ unless (cid == 0) $ do
-                      pipe <- asks mongoPipe
-                      liftIOE ConnectionFailure $ P.send pipe [KillCursors [cid]]
-                    return (dBatch', Just doc)
-                [] -> if cid == 0
-                    then return (return $ Batch (Just 0) 0 [], Nothing)  -- finished
-                    else do
-                      nb <- nextBatch' fcol batchSize mLimit cid
-                      return (nb, Nothing)
-
-nextN :: MonadIO m => Int -> Cursor -> Action m [Document]
--- ^ Return next N documents or less if end is reached
-nextN n c = catMaybes `liftM` replicateM n (next c)
-
-rest :: MonadIO m => Cursor -> Action m [Document]
--- ^ Return remaining documents in query result
-rest c = loop (next c)
-
-closeCursor :: MonadIO m => Cursor -> Action m ()
-closeCursor (Cursor _ _ var) = liftDB $ modifyMVar var $ \dBatch -> do
-    Batch _ cid _ <- fulfill dBatch
-    unless (cid == 0) $ do
-      pipe <- asks mongoPipe
-      liftIOE ConnectionFailure $ P.send pipe [KillCursors [cid]]
-    return $ (return $ Batch (Just 0) 0 [], ())
-
-isCursorClosed :: MonadIO m => Cursor -> Action m Bool
-isCursorClosed (Cursor _ _ var) = do
-        Batch _ cid docs <- liftDB $ fulfill =<< readMVar var
-        return (cid == 0 && null docs)
-
--- ** Aggregate
-
-type Pipeline = [Document]
--- ^ The Aggregate Pipeline
-
-aggregate :: MonadIO m => Collection -> Pipeline -> Action m [Document]
--- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
-aggregate aColl agg = do
-    aggregateCursor aColl agg def >>= rest
-
-data AggregateConfig = AggregateConfig {}
-                       deriving Show
-
-instance Default AggregateConfig where
-  def = AggregateConfig {}
-
-aggregateCursor :: MonadIO m => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
--- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
-aggregateCursor aColl agg _ = do
-    response <- runCommand ["aggregate" =: aColl, "pipeline" =: agg, "cursor" =: ([] :: Document)]
-    case true1 "ok" response of
-        True  -> do
-          cursor :: Document <- lookup "cursor" response
-          firstBatch :: [Document] <- lookup "firstBatch" cursor
-          cursorId :: Int64 <- lookup "id" cursor
-          db <- thisDatabase
-          newCursor db aColl 0 $ return $ Batch Nothing cursorId  firstBatch
-        False -> liftIO $ throwIO $ AggregateFailure $ at "errmsg" response
-
--- ** Group
-
--- | Groups documents in collection by key then reduces (aggregates) each group
-data Group = Group {
-    gColl :: Collection,
-    gKey :: GroupKey,  -- ^ Fields to group by
-    gReduce :: Javascript,  -- ^ @(doc, agg) -> ()@. The reduce function reduces (aggregates) the objects iterated. Typical operations of a reduce function include summing and counting. It takes two arguments, the current document being iterated over and the aggregation value, and updates the aggregate value.
-    gInitial :: Document,  -- ^ @agg@. Initial aggregation value supplied to reduce
-    gCond :: Selector,  -- ^ Condition that must be true for a row to be considered. [] means always true.
-    gFinalize :: Maybe Javascript  -- ^ @agg -> () | result@. 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).
-    } deriving (Show, Eq)
-
-data GroupKey = Key [Label] | KeyF Javascript  deriving (Show, Eq)
--- ^ Fields to group by, or function (@doc -> key@) returning a "key object" to be used as the grouping key. Use KeyF 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
-groupDocument Group{..} =
-    ("finalize" =? gFinalize) ++ [
-    "ns" =: gColl,
-    case gKey of Key k -> "key" =: map (=: True) k; KeyF f -> "$keyf" =: f,
-    "$reduce" =: gReduce,
-    "initial" =: gInitial,
-    "cond" =: gCond ]
-
-group :: (MonadIO m) => Group -> Action m [Document]
--- ^ Execute group query and return resulting aggregate value for each distinct key
-group g = at "retval" `liftM` runCommand ["group" =: groupDocument g]
-
--- ** MapReduce
-
--- | Maps every document in collection to a list of (key, value) pairs, then for each unique key reduces all its associated values to a single result. There are additional parameters that may be set to tweak this basic operation.
--- This implements the latest version of map-reduce that requires MongoDB 1.7.4 or greater. To map-reduce against an older server use runCommand directly as described in http://www.mongodb.org/display/DOCS/MapReduce.
-data MapReduce = MapReduce {
-    rColl :: Collection,
-    rMap :: MapFun,
-    rReduce :: ReduceFun,
-    rSelect :: Selector,  -- ^ Operate on only those documents selected. Default is [] meaning all documents.
-    rSort :: Order,  -- ^ Default is [] meaning no sort
-    rLimit :: Limit,  -- ^ Default is 0 meaning no limit
-    rOut :: MROut,  -- ^ Output to a collection with a certain merge policy. Default is no collection ('Inline'). Note, you don't want this default if your result set is large.
-    rFinalize :: Maybe FinalizeFun,  -- ^ 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)
-
-type MapFun = Javascript
--- ^ @() -> void@. The map function references the variable @this@ to inspect the current object under consideration. The function must call @emit(key,value)@ at least once, but may be invoked any number of times, as may be appropriate.
-
-type ReduceFun = Javascript
--- ^ @(key, [value]) -> value@. The reduce function receives a key and an array of values and returns an aggregate result value. The MapReduce engine may invoke reduce functions iteratively; thus, these functions must be idempotent.  That is, the following must hold for your reduce function: @reduce(k, [reduce(k,vs)]) == reduce(k,vs)@. 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.
-
-type FinalizeFun = 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.
-
-data MROut =
-      Inline -- ^ Return results directly instead of writing them to an output collection. Results must fit within 16MB limit of a single document
-    | Output MRMerge Collection (Maybe Database) -- ^ Write results to given collection, in other database if specified. Follow merge policy when entry already exists
-    deriving (Show, Eq)
-
-data MRMerge =
-      Replace  -- ^ Clear all old data and replace it with new data
-    | Merge  -- ^ Leave old data but overwrite entries with the same key with new data
-    | Reduce  -- ^ Leave old data but combine entries with the same key via MR's reduce function
-    deriving (Show, Eq)
-
-type MRResult = Document
--- ^ Result of running a MapReduce has some stats besides the output. See http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Resultobject
-
-mrDocument :: MapReduce -> Document
--- ^ Translate MapReduce data into expected document form
-mrDocument MapReduce{..} =
-    ("mapreduce" =: rColl) :
-    ("out" =: mrOutDoc rOut) :
-    ("finalize" =? rFinalize) ++ [
-    "map" =: rMap,
-    "reduce" =: rReduce,
-    "query" =: rSelect,
-    "sort" =: rSort,
-    "limit" =: (fromIntegral rLimit :: Int),
-    "scope" =: rScope,
-    "verbose" =: rVerbose ]
-
-mrOutDoc :: MROut -> Document
--- ^ Translate MROut into expected document form
-mrOutDoc Inline = ["inline" =: (1 :: Int)]
-mrOutDoc (Output mrMerge coll mDB) = (mergeName mrMerge =: coll) : mdb mDB where
-    mergeName Replace = "replace"
-    mergeName Merge = "merge"
-    mergeName Reduce = "reduce"
-    mdb Nothing = []
-    mdb (Just db) = ["db" =: db]
-
-mapReduce :: Collection -> MapFun -> ReduceFun -> MapReduce
--- ^ MapReduce on collection with given map and reduce functions. Remaining attributes are set to their defaults, which are stated in their comments.
-mapReduce col map' red = MapReduce col map' red [] [] 0 Inline Nothing [] False
-
-runMR :: MonadIO m => MapReduce -> Action m Cursor
--- ^ Run MapReduce and return cursor of results. Error if map/reduce fails (because of bad Javascript)
-runMR mr = do
-    res <- runMR' mr
-    case look "result" res of
-        Just (String coll) -> find $ query [] coll
-        Just (Doc doc) -> useDb (at "db" doc) $ find $ query [] (at "collection" doc)
-        Just x -> error $ "unexpected map-reduce result field: " ++ show x
-        Nothing -> newCursor "" "" 0 $ return $ Batch (Just 0) 0 (at "results" res)
-
-runMR' :: (MonadIO m) => MapReduce -> Action m MRResult
--- ^ Run MapReduce and return a MR result document containing stats and the results if Inlined. Error if the map/reduce failed (because of bad Javascript).
-runMR' mr = do
-    doc <- runCommand (mrDocument mr)
-    return $ if true1 "ok" doc then doc else error $ "mapReduce error:\n" ++ show doc ++ "\nin:\n" ++ show mr
-
--- * Command
-
-type Command = Document
--- ^ A command is a special query or action against the database. See <http://www.mongodb.org/display/DOCS/Commands> for details.
-
-runCommand :: (MonadIO m) => Command -> Action m Document
--- ^ Run command against the database and return its result
-runCommand c = maybe err id `liftM` findOne (query c "$cmd") where
-    err = error $ "Nothing returned for command: " ++ show c
-
-runCommand1 :: (MonadIO m) => Text -> Action m Document
--- ^ @runCommand1 foo = runCommand [foo =: 1]@
-runCommand1 c = runCommand [c =: (1 :: Int)]
-
-eval :: (MonadIO m, Val v) => Javascript -> Action m v
--- ^ Run code on server
-eval code = at "retval" `liftM` runCommand ["$eval" =: code]
-
-modifyMVar :: MVar a -> (a -> Action IO (a, b)) -> Action IO b
-modifyMVar v f = do
-  ctx <- ask
-  liftIO $ MV.modifyMVar v (\x -> runReaderT (f x) ctx)
-
-mkWeakMVar :: MVar a -> Action IO () -> Action IO (Weak (MVar a))
-mkWeakMVar m closing = do
-  ctx <- ask
+{-# LANGUAGE OverloadedStrings, RecordWildCards, NamedFieldPuns, TupleSections, FlexibleContexts, FlexibleInstances, UndecidableInstances, MultiParamTypeClasses, TypeFamilies, CPP, DeriveDataTypeable, ScopedTypeVariables, BangPatterns #-}
+
+module Database.MongoDB.Query (
+    -- * Monad
+    Action, access, Failure(..), ErrorCode,
+    AccessMode(..), GetLastError, master, slaveOk, accessMode,
+    liftDB,
+    MongoContext(..), HasMongoContext(..),
+    -- * Database
+    Database, allDatabases, useDb, thisDatabase,
+    -- ** Authentication
+    Username, Password, auth, authMongoCR, authSCRAMSHA1, authSCRAMSHA256,
+    -- * Collection
+    Collection, allCollections,
+    -- ** Selection
+    Selection(..), Selector, whereJS,
+    Select(select),
+    -- * Write
+    -- ** Insert
+    insert, insert_, insertMany, insertMany_, insertAll, insertAll_,
+    -- ** Update
+    save, replace, repsert, upsert, Modifier, modify, updateMany, updateAll,
+    WriteResult(..), UpdateOption(..), Upserted(..),
+    -- ** Delete
+    delete, deleteOne, deleteMany, deleteAll, DeleteOption(..),
+    -- * Read
+    -- ** Query
+    Query(..), QueryOption(NoCursorTimeout, TailableCursor, AwaitData, Partial),
+    Projector, Limit, Order, BatchSize,
+    explain, find, findCommand, findOne, fetch,
+    findAndModify, findAndModifyOpts, FindAndModifyOpts(..), defFamUpdateOpts,
+    count, distinct,
+    -- *** Cursor
+    Cursor, nextBatch, next, nextN, rest, closeCursor, isCursorClosed,
+    -- ** Aggregate
+    Pipeline, AggregateConfig(..), aggregate, aggregateCursor,
+    -- ** Group
+    Group(..), GroupKey(..), group,
+    -- ** MapReduce
+    MapReduce(..), MapFun, ReduceFun, FinalizeFun, MROut(..), MRMerge(..),
+    MRResult, mapReduce, runMR, runMR',
+    -- * Command
+    Command, runCommand, runCommand1,
+    eval, retrieveServerData, ServerData(..)
+) where
+
+import qualified Control.Concurrent.MVar as MV
+import Control.Concurrent.MVar.Lifted
+  ( MVar,
+    readMVar,
+  )
+import Control.Exception (Exception, catch, throwIO)
+import Control.Monad
+  ( liftM2,
+    replicateM,
+    unless,
+    void,
+    when,
+  )
+import Control.Monad.Reader (MonadReader, ReaderT, ask, asks, local, runReaderT)
+import Control.Monad.Trans (MonadIO, liftIO, lift)
+import Control.Monad.Trans.Except
+import qualified Crypto.Hash.MD5 as MD5
+import qualified Crypto.Hash.SHA1 as SHA1
+import qualified Crypto.Hash.SHA256 as SHA256
+import qualified Crypto.MAC.HMAC as HMAC
+import qualified Crypto.Nonce as Nonce
+import Data.Binary.Put (runPut)
+import Data.Bits (xor)
+import Data.Bson
+  ( Document,
+    Field (..),
+    Javascript,
+    Label,
+    ObjectId,
+    Val (..),
+    Value (..),
+    at,
+    genObjectId,
+    look,
+    lookup,
+    valueAt,
+    (!?),
+    (=:),
+    (=?),
+    merge,
+    cast
+  )
+import Data.Bson.Binary (putDocument)
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Base16 as B16
+import qualified Data.ByteString.Base64 as B64
+import qualified Data.ByteString.Char8 as B
+import qualified Data.ByteString.Lazy as LBS
+import Data.Default.Class (Default (..))
+import Data.Either (lefts, rights)
+import qualified Data.Either as E
+import Data.Functor ((<&>))
+import Data.Int (Int32, Int64)
+import Data.List (foldl1')
+import qualified Data.Map as Map
+import Data.Maybe (catMaybes, fromMaybe, isNothing, listToMaybe, mapMaybe, maybeToList, fromJust)
+import Data.Text (Text)
+import qualified Data.Text as T
+import Data.Typeable (Typeable)
+import Data.Word (Word32)
+import Database.MongoDB.Internal.Protocol
+  ( CursorId,
+    DeleteOption (..),
+    FullCollection,
+    InsertOption (..),
+    Notice (..),
+    Password,
+    Pipe,
+    QueryOption (..),
+    Reply (..),
+    Request
+      ( GetMore,
+        qBatchSize,
+        qFullCollection,
+        qOptions,
+        qProjector,
+        qSelector,
+        qSkip
+      ),
+    ResponseFlag (..),
+    ServerData (..),
+    UpdateOption (..),
+    Username,
+    Cmd (..),
+    pwKey,
+    FlagBit (..)
+  )
+import Control.Monad.Trans.Except
+import qualified Database.MongoDB.Internal.Protocol as P
+import Database.MongoDB.Internal.Util (liftIOE, loop, true1, (<.>), splitDot)
+import System.Mem.Weak (Weak)
+import Text.Read (readMaybe)
+import Prelude hiding (lookup)
+
+-- * Monad
+
+type Action = ReaderT MongoContext
+-- ^ A monad on top of m (which must be a MonadIO) that may access the database and may fail with a DB 'Failure'
+
+access :: (MonadIO m) => Pipe -> AccessMode -> Database -> Action m a -> m a
+-- ^ Run action against database on server at other end of pipe. Use access mode for any reads and writes.
+-- Throw 'Failure' in case of any error.
+access mongoPipe mongoAccessMode mongoDatabase action = runReaderT action MongoContext{..}
+
+-- | A connection failure, or a read or write exception like cursor expired or inserting a duplicate key.
+-- Note, unexpected data from the server is not a Failure, rather it is a programming error (you should call 'error' in this case) because the client and server are incompatible and requires a programming change.
+data Failure =
+     ConnectionFailure IOError  -- ^ TCP connection ('Pipeline') failed. May work if you try again on the same Mongo 'Connection' which will create a new Pipe.
+    | CursorNotFoundFailure CursorId  -- ^ Cursor expired because it wasn't accessed for over 10 minutes, or this cursor came from a different server that the one you are currently connected to (perhaps a fail over happen between servers in a replica set)
+    | QueryFailure ErrorCode String  -- ^ Query failed for some reason as described in the string
+    | WriteFailure Int ErrorCode String -- ^ Error observed by getLastError after a write, error description is in string, index of failed document is the first argument
+    | WriteConcernFailure Int String  -- ^ Write concern error. It's reported only by insert, update, delete commands. Not by wire protocol.
+    | DocNotFound Selection  -- ^ 'fetch' found no document matching selection
+    | AggregateFailure String -- ^ 'aggregate' returned an error
+    | CompoundFailure [Failure] -- ^ When we need to aggregate several failures and report them.
+    | ProtocolFailure Int String -- ^ The structure of the returned documents doesn't match what we expected
+    deriving (Show, Eq, Typeable)
+instance Exception Failure
+
+type ErrorCode = Int
+-- ^ Error code from @getLastError@ or query failure.
+
+-- | Type of reads and writes to perform.
+data AccessMode =
+     ReadStaleOk  -- ^ Read-only action, reading stale data from a slave is OK.
+    | UnconfirmedWrites  -- ^ Read-write action, slave not OK, every write is fire & forget.
+    | ConfirmWrites GetLastError  -- ^ Read-write action, slave not OK, every write is confirmed with @getLastError@.
+    deriving Show
+
+type GetLastError = Document
+-- ^ Parameters for @getLastError@ command. For example @[\"w\" =: 2]@ tells the server to wait for the write to reach at least two servers in replica set before acknowledging. See <http://www.mongodb.org/display/DOCS/Last+Error+Commands> for more options.
+
+class Result a where
+  isFailed :: a -> Bool
+
+data WriteResult = WriteResult
+                  { failed      :: Bool
+                  , nMatched    :: Int
+                  , nModified   :: Maybe Int
+                  , nRemoved    :: Int
+                  -- ^ Mongodb server before 2.6 doesn't allow to calculate this value.
+                  -- This field is meaningless if we can't calculate the number of modified documents.
+                  , upserted    :: [Upserted]
+                  , writeErrors :: [Failure]
+                  , writeConcernErrors :: [Failure]
+                  } deriving Show
+
+instance Result WriteResult where
+  isFailed = failed
+
+instance Result (Either a b) where
+  isFailed (Left _) = True
+  isFailed _ = False
+
+data Upserted = Upserted
+              { upsertedIndex :: Int
+              , upsertedId    :: ObjectId
+              } deriving Show
+
+master :: AccessMode
+-- ^ Same as 'ConfirmWrites' []
+master = ConfirmWrites []
+
+slaveOk :: AccessMode
+-- ^ Same as 'ReadStaleOk'
+slaveOk = ReadStaleOk
+
+accessMode :: (Monad m) => AccessMode -> Action m a -> Action m a
+-- ^ Run action with given 'AccessMode'
+accessMode mode = local (\ctx -> ctx {mongoAccessMode = mode})
+
+readMode :: AccessMode -> ReadMode
+readMode ReadStaleOk = StaleOk
+readMode _ = Fresh
+
+writeMode :: AccessMode -> WriteMode
+writeMode ReadStaleOk = Confirm []
+writeMode UnconfirmedWrites = NoConfirm
+writeMode (ConfirmWrites z) = Confirm z
+
+-- | Values needed when executing a db operation
+data MongoContext = MongoContext {
+    mongoPipe :: Pipe, -- ^ operations read/write to this pipelined TCP connection to a MongoDB server
+    mongoAccessMode :: AccessMode, -- ^ read/write operation will use this access mode
+    mongoDatabase :: Database -- ^ operations query/update this database
+}
+
+mongoReadMode :: MongoContext -> ReadMode
+mongoReadMode = readMode . mongoAccessMode
+
+mongoWriteMode :: MongoContext -> WriteMode
+mongoWriteMode = writeMode . mongoAccessMode
+
+class HasMongoContext env where
+    mongoContext :: env -> MongoContext
+instance HasMongoContext MongoContext where
+    mongoContext = id
+
+liftDB :: (MonadReader env m, HasMongoContext env, MonadIO m)
+       => Action IO a
+       -> m a
+liftDB m = do
+    env <- ask
+    liftIO $ runReaderT m (mongoContext env)
+
+-- * Database
+
+type Database = Text
+
+allDatabases :: (MonadIO m) => Action m [Database]
+-- ^ List all databases residing on server
+allDatabases = map (at "name") . at "databases" <$> useDb "admin" (runCommand1 "listDatabases")
+
+thisDatabase :: (Monad m) => Action m Database
+-- ^ Current database in use
+thisDatabase = asks mongoDatabase
+
+useDb :: (Monad m) => Database -> Action m a -> Action m a
+-- ^ Run action against given database
+useDb db = local (\ctx -> ctx {mongoDatabase = db})
+
+-- * Authentication
+
+auth :: MonadIO m => Username -> Password -> Action m Bool
+-- ^ Authenticate with the current database (if server is running in secure mode). Return whether authentication was successful or not. Reauthentication is required for every new pipe. SCRAM-SHA-1 will be used for server versions 3.0+, MONGO-CR for lower versions.
+auth un pw = do
+    let serverVersion = fmap (at "version") $ useDb "admin" $ runCommand ["buildinfo" =: (1 :: Int)]
+    mmv <- readMaybe . T.unpack . head . T.splitOn "." <$> serverVersion
+    maybe (return False) performAuth mmv
+    where
+    performAuth majorVersion =
+        if majorVersion >= (3 :: Int)
+        then authSCRAMSHA1 un pw
+        else authMongoCR un pw
+
+authMongoCR :: (MonadIO m) => Username -> Password -> Action m Bool
+-- ^ Authenticate with the current database, using the MongoDB-CR authentication mechanism (default in MongoDB server < 3.0)
+authMongoCR usr pss = do
+    n <- at "nonce" <$> runCommand ["getnonce" =: (1 :: Int)]
+    true1 "ok" <$> runCommand ["authenticate" =: (1 :: Int), "user" =: usr, "nonce" =: n, "key" =: pwKey n usr pss]
+
+data HashAlgorithm = SHA1 | SHA256 deriving Show
+
+hash :: HashAlgorithm -> B.ByteString -> B.ByteString
+hash SHA1 = SHA1.hash
+hash SHA256 = SHA256.hash
+
+authSCRAMSHA1 :: MonadIO m => Username -> Password -> Action m Bool
+authSCRAMSHA1 = authSCRAMWith SHA1
+
+authSCRAMSHA256 :: MonadIO m => Username -> Password -> Action m Bool
+authSCRAMSHA256 = authSCRAMWith SHA256
+
+toAuthResult :: Functor m => ExceptT String (Action m) () -> Action m Bool
+toAuthResult = fmap (either (const False) (const True)) . runExceptT
+
+-- | It should technically perform SASLprep, but the implementation is currently id
+saslprep :: Text -> Text
+saslprep = id
+
+authSCRAMWith :: MonadIO m => HashAlgorithm -> Username -> Password -> Action m Bool
+-- ^ Authenticate with the current database, using the SCRAM-SHA-1 authentication mechanism (default in MongoDB server >= 3.0)
+authSCRAMWith algo un pw = toAuthResult $ do
+    let hmac = HMAC.hmac (hash algo) 64
+    nonce <- liftIO (Nonce.withGenerator Nonce.nonce128 <&> B64.encode)
+    let firstBare = B.concat [B.pack $ "n=" ++ T.unpack un ++ ",r=", nonce]
+    let client1 =
+          [ "saslStart" =: (1 :: Int)
+          , "mechanism" =: case algo of
+            SHA1 -> "SCRAM-SHA-1" :: String
+            SHA256 -> "SCRAM-SHA-256"
+          , "payload" =: (B.unpack . B64.encode $ B.concat [B.pack "n,,", firstBare])
+          , "autoAuthorize" =: (1 :: Int)
+          ]
+    server1 <- lift $ runCommand client1
+
+    shortcircuit (true1 "ok" server1) (show server1)
+    let serverPayload1 = B64.decodeLenient . B.pack . at "payload" $ server1
+    let serverData1 = parseSCRAM serverPayload1
+    let iterations = read . B.unpack $ Map.findWithDefault "1" "i" serverData1
+    let salt = B64.decodeLenient $ Map.findWithDefault "" "s" serverData1
+    let snonce = Map.findWithDefault "" "r" serverData1
+
+    shortcircuit (B.isInfixOf nonce snonce) "nonce"
+    let withoutProof = B.concat [B.pack "c=biws,r=", snonce]
+    let digest = case algo of
+          SHA1 -> B16.encode $ MD5.hash $ B.pack $ T.unpack un ++ ":mongo:" ++ T.unpack pw
+          SHA256 -> B.pack $ T.unpack $ saslprep pw
+    let saltedPass = scramHI algo digest salt iterations
+    let clientKey = hmac saltedPass (B.pack "Client Key")
+    let storedKey = hash algo clientKey
+    let authMsg = B.concat [firstBare, B.pack ",", serverPayload1, B.pack ",", withoutProof]
+    let clientSig = hmac storedKey authMsg
+    let pval = B64.encode . BS.pack $ BS.zipWith xor clientKey clientSig
+    let clientFinal = B.concat [withoutProof, B.pack ",p=", pval]
+
+    let client2 =
+            [ "saslContinue" =: (1 :: Int)
+            , "conversationId" =: (at "conversationId" server1 :: Int)
+            , "payload" =: B.unpack (B64.encode clientFinal)
+            ]
+    server2 <- lift $ runCommand client2
+    shortcircuit (true1 "ok" server2) (show server2)
+
+    let serverKey = hmac saltedPass (B.pack "Server Key")
+    let serverSig = B64.encode $ hmac serverKey authMsg
+    let serverPayload2 = B64.decodeLenient . B.pack $ at "payload" server2
+    let serverData2 = parseSCRAM serverPayload2
+    let serverSigComp = Map.findWithDefault "" "v" serverData2
+
+    shortcircuit (serverSig == serverSigComp) "server signature does not match"
+    if true1 "done" server2
+      then return ()
+      else do
+        let client2Step2 = [ "saslContinue" =: (1 :: Int)
+                            , "conversationId" =: (at "conversationId" server1 :: Int)
+                            , "payload" =: String ""]
+        server3 <- lift $ runCommand client2Step2
+        shortcircuit (true1 "ok" server3) "server3"
+
+shortcircuit :: Monad m => Bool -> String -> ExceptT String m ()
+shortcircuit True _ = pure ()
+shortcircuit False reason = throwE (show reason)
+
+scramHI :: HashAlgorithm -> B.ByteString -> B.ByteString -> Int -> B.ByteString
+scramHI algo digest salt iters = snd $ foldl com (u1, u1) [1..(iters-1)]
+    where
+    hmacd = HMAC.hmac (hash algo) 64 digest
+    u1 = hmacd (B.concat [salt, BS.pack [0, 0, 0, 1]])
+    com (u,uc) _ = let u' = hmacd u in (u', BS.pack $ BS.zipWith xor uc u')
+
+parseSCRAM :: B.ByteString -> Map.Map B.ByteString B.ByteString
+parseSCRAM = Map.fromList . fmap (cleanup . T.breakOn "=") . T.splitOn "," . T.pack . B.unpack
+    where cleanup (t1, t2) = (B.pack $ T.unpack t1, B.pack . T.unpack $ T.drop 1 t2)
+
+-- As long as server api  is not requested OP_Query has to be used. See:
+-- https://github.com/mongodb/specifications/blob/6dc6f80026f0f8d99a8c81f996389534b14f6602/source/mongodb-handshake/handshake.rst#specification
+retrieveServerData :: (MonadIO m) => Action m ServerData
+retrieveServerData = do
+  d <- runCommand1 "isMaster"
+  let newSd = ServerData
+                { isMaster = fromMaybe False $ lookup "isMaster" d
+                , minWireVersion = fromMaybe 0 $ lookup "minWireVersion" d
+                , maxWireVersion = fromMaybe 0 $ lookup "maxWireVersion" d
+                , maxMessageSizeBytes = fromMaybe 48000000 $ lookup "maxMessageSizeBytes" d
+                , maxBsonObjectSize = fromMaybe (16 * 1024 * 1024) $ lookup "maxBsonObjectSize" d
+                , maxWriteBatchSize = fromMaybe 1000 $ lookup "maxWriteBatchSize" d
+                }
+  return newSd
+
+-- * Collection
+
+type Collection = Text
+-- ^ Collection name (not prefixed with database)
+
+allCollections :: MonadIO m => Action m [Collection]
+-- ^ List all collections in this database
+allCollections = do
+    p <- asks mongoPipe
+    let sd = P.serverData p
+    if maxWireVersion sd <= 2
+      then do
+        db <- thisDatabase
+        docs <- rest =<< find (query [] "system.namespaces") {sort = ["name" =: (1 :: Int)]}
+        (return . filter (not . isSpecial db)) (map (dropDbPrefix . at "name") docs)
+      else
+        if maxWireVersion sd < 17
+          then do
+            r <- runCommand1 "listCollections"
+            let curData = do
+                       (Doc curDoc) <- r !? "cursor"
+                       (curId :: Int64) <- curDoc !? "id"
+                       (curNs :: Text) <- curDoc !? "ns"
+                       (firstBatch :: [Value]) <- curDoc !? "firstBatch"
+                       return (curId, curNs, mapMaybe cast' firstBatch :: [Document])
+            case curData of
+              Nothing -> return []
+              Just (curId, curNs, firstBatch) -> do
+                db <- thisDatabase
+                nc <- newCursor db curNs 0 $ return $ Batch Nothing curId firstBatch
+                docs <- rest nc
+                return $ mapMaybe (\d -> d !? "name") docs
+          else do
+            let q = Query [] (Select ["listCollections" =: (1 :: Int)] "$cmd") [] 0 0 [] False 0 []
+            qr <- queryRequestOpMsg False q
+            dBatch <- liftIO $ requestOpMsg p qr []
+            db <- thisDatabase
+            nc <- newCursor db "$cmd" 0 dBatch
+            docs <- rest nc
+            return $ mapMaybe (\d -> d !? "name") docs
+ where
+    dropDbPrefix = T.tail . T.dropWhile (/= '.')
+    isSpecial db col = T.any (== '$') col && db <.> col /= "local.oplog.$main"
+
+-- * Selection
+
+data Selection = Select {selector :: Selector, coll :: Collection}  deriving (Show, Eq)
+-- ^ Selects documents in collection that match selector
+
+type Selector = 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
+whereJS sel js = ("$where" =: js) : sel
+
+class Select aQueryOrSelection where
+    select :: Selector -> Collection -> aQueryOrSelection
+    -- ^ 'Query' or 'Selection' that selects documents in collection that match selector. The choice of type depends on use, for example, in @'find' (select sel col)@ it is a Query, and in @'delete' (select sel col)@ it is a Selection.
+
+instance Select Selection where
+    select = Select
+
+instance Select Query where
+    select = query
+
+-- * Write
+
+data WriteMode =
+      NoConfirm  -- ^ Submit writes without receiving acknowledgments. Fast. Assumes writes succeed even though they may not.
+    | Confirm GetLastError  -- ^ Receive an acknowledgment after every write, and raise exception if one says the write failed. This is acomplished by sending the getLastError command, with given 'GetLastError' parameters, after every write.
+    deriving (Show, Eq)
+
+write :: Notice -> Action IO (Maybe Document)
+-- ^ Send write to server, and if write-mode is 'Safe' then include getLastError request and raise 'WriteFailure' if it reports an error.
+write notice = asks mongoWriteMode >>= \mode -> case mode of
+    NoConfirm -> do
+      pipe <- asks mongoPipe
+      liftIOE ConnectionFailure $ P.send pipe [notice]
+      return Nothing
+    Confirm params -> do
+        let q = query (("getlasterror" =: (1 :: Int)) : params) "$cmd"
+        pipe <- asks mongoPipe
+        Batch _ _ [doc] <- do
+          r <- queryRequest False q {limit = 1}
+          rr <- liftIO $ request pipe [notice] r
+          fulfill rr
+        return $ Just doc
+
+-- ** Insert
+
+insert :: (MonadIO m) => Collection -> Document -> Action m Value
+-- ^ Insert document into collection and return its \"_id\" value, which is created automatically if not supplied
+insert col doc = do
+  doc' <- liftIO $ assignId doc
+  res <- insertBlock [] col (0, [doc'])
+  case res of
+    Left failure -> liftIO $ throwIO failure
+    Right r -> return $ head r
+
+insert_ :: (MonadIO m) => Collection -> Document -> Action m ()
+-- ^ Same as 'insert' except don't return _id
+insert_ col doc = insert col doc >> return ()
+
+insertMany :: (MonadIO m) => Collection -> [Document] -> Action m [Value]
+-- ^ Insert documents into collection and return their \"_id\" values,
+-- which are created automatically if not supplied.
+-- If a document fails to be inserted (eg. due to duplicate key)
+-- then remaining docs are aborted, and @LastError@ is set.
+-- An exception will be throw if any error occurs.
+insertMany = insert' []
+
+insertMany_ :: (MonadIO m) => Collection -> [Document] -> Action m ()
+-- ^ Same as 'insertMany' except don't return _ids
+insertMany_ col docs = insertMany col docs >> return ()
+
+insertAll :: (MonadIO m) => Collection -> [Document] -> Action m [Value]
+-- ^ Insert documents into collection and return their \"_id\" values,
+-- which are created automatically if not supplied. If a document fails
+-- to be inserted (eg. due to duplicate key) then remaining docs
+-- are still inserted.
+insertAll = insert' [KeepGoing]
+
+insertAll_ :: (MonadIO m) => Collection -> [Document] -> Action m ()
+-- ^ Same as 'insertAll' except don't return _ids
+insertAll_ col docs = insertAll col docs >> return ()
+
+insertCommandDocument :: [InsertOption] -> Collection -> [Document] -> Document -> Document
+insertCommandDocument opts col docs writeConcern =
+          [ "insert" =: col
+          , "ordered" =: (KeepGoing `notElem` opts)
+          , "documents" =: docs
+          , "writeConcern" =: writeConcern
+          ]
+
+takeRightsUpToLeft :: [Either a b] -> [b]
+takeRightsUpToLeft l = E.rights $ takeWhile E.isRight l
+
+insert' :: (MonadIO m)
+        => [InsertOption] -> Collection -> [Document] -> Action m [Value]
+-- ^ Insert documents into collection and return their \"_id\" values, which are created automatically if not supplied
+insert' opts col docs = do
+  p <- asks mongoPipe
+  let sd = P.serverData p
+  docs' <- liftIO $ mapM assignId docs
+  mode <- asks mongoWriteMode
+  let writeConcern = case mode of
+                        NoConfirm -> ["w" =: (0 :: Int32)]
+                        Confirm params -> params
+  let docSize = sizeOfDocument $ insertCommandDocument opts col [] writeConcern
+  let ordered = KeepGoing `notElem` opts
+  let preChunks = splitAtLimit
+                      (maxBsonObjectSize sd - docSize)
+                                           -- size of auxiliary part of insert
+                                           -- document should be subtracted from
+                                           -- the overall size
+                      (maxWriteBatchSize sd)
+                      docs'
+  let chunks =
+        if ordered
+            then takeRightsUpToLeft preChunks
+            else rights preChunks
+
+  let lens = map length chunks
+  let lSums = 0 : zipWith (+) lSums lens
+
+  chunkResults <- interruptibleFor ordered (zip lSums chunks) $ insertBlock opts col
+
+  let lchunks = lefts preChunks
+  when (not $ null lchunks) $ do
+    liftIO $ throwIO $ head lchunks
+
+  let lresults = lefts chunkResults
+  when (not $ null lresults) $ liftIO $ throwIO $ head lresults
+  return $ concat $ rights chunkResults
+
+insertBlock :: (MonadIO m)
+            => [InsertOption] -> Collection -> (Int, [Document]) -> Action m (Either Failure [Value])
+-- ^ This will fail if the list of documents is bigger than restrictions
+insertBlock _ _ (_, []) = return $ Right []
+insertBlock opts col (prevCount, docs) = do
+    db <- thisDatabase
+
+    p <- asks mongoPipe
+    let sd = P.serverData p
+    if maxWireVersion sd < 2
+      then do
+        res <- liftDB $ write (Insert (db <.> col) opts docs)
+        let errorMessage = do
+              jRes <- res
+              em <- lookup "err" jRes
+              return $ WriteFailure prevCount (fromMaybe 0 $ lookup "code" jRes)  em
+              -- In older versions of ^^ the protocol we can't really say which document failed.
+              -- So we just report the accumulated number of documents in the previous blocks.
+
+        case errorMessage of
+          Just failure -> return $ Left failure
+          Nothing -> return $ Right $ map (valueAt "_id") docs
+      else if maxWireVersion sd == 2 && maxWireVersion sd < 17 then do
+        mode <- asks mongoWriteMode
+        let writeConcern = case mode of
+                              NoConfirm -> ["w" =: (0 :: Int32)]
+                              Confirm params -> params
+        doc <- runCommand $ insertCommandDocument opts col docs writeConcern
+        case (look "writeErrors" doc, look "writeConcernError" doc) of
+          (Nothing, Nothing) -> return $ Right $ map (valueAt "_id") docs
+          (Just (Array errs), Nothing) -> do
+            let writeErrors = map (anyToWriteError prevCount) errs
+            let errorsWithFailureIndex = map (addFailureIndex prevCount) writeErrors
+            return $ Left $ CompoundFailure errorsWithFailureIndex
+          (Nothing, Just err) -> do
+            return $ Left $ WriteFailure
+                                    prevCount
+                                    (fromMaybe 0 $ lookup "ok" doc)
+                                    (show err)
+          (Just (Array errs), Just writeConcernErr) -> do
+            let writeErrors = map (anyToWriteError prevCount) errs
+            let errorsWithFailureIndex = map (addFailureIndex prevCount) writeErrors
+            return $ Left $ CompoundFailure $ WriteFailure
+                                    prevCount
+                                    (fromMaybe 0 $ lookup "ok" doc)
+                                    (show writeConcernErr) : errorsWithFailureIndex
+          (Just unknownValue, Nothing) -> do
+            return $ Left $ ProtocolFailure prevCount $ "Expected array of errors. Received: " ++ show unknownValue
+          (Just unknownValue, Just writeConcernErr) -> do
+            return $ Left $ CompoundFailure [ ProtocolFailure prevCount $ "Expected array of errors. Received: " ++ show unknownValue
+                                              , WriteFailure prevCount (fromMaybe 0 $ lookup "ok" doc) $ show writeConcernErr]
+      else do
+        mode <- asks mongoWriteMode
+        let writeConcern = case mode of
+                              NoConfirm -> ["w" =: (0 :: Int32)]
+                              Confirm params -> merge params ["w" =: (1 :: Int32)]
+        doc <- runCommand $ insertCommandDocument opts col docs writeConcern
+        case (look "writeErrors" doc, look "writeConcernError" doc) of
+          (Nothing, Nothing) -> return $ Right $ map (valueAt "_id") docs
+          (Just (Array errs), Nothing) -> do
+            let writeErrors = map (anyToWriteError prevCount) errs
+            let errorsWithFailureIndex = map (addFailureIndex prevCount) writeErrors
+            return $ Left $ CompoundFailure errorsWithFailureIndex
+          (Nothing, Just err) -> do
+            return $ Left $ WriteFailure
+                                    prevCount
+                                    (fromMaybe 0 $ lookup "ok" doc)
+                                    (show err)
+          (Just (Array errs), Just writeConcernErr) -> do
+            let writeErrors = map (anyToWriteError prevCount) errs
+            let errorsWithFailureIndex = map (addFailureIndex prevCount) writeErrors
+            return $ Left $ CompoundFailure $ WriteFailure
+                                    prevCount
+                                    (fromMaybe 0 $ lookup "ok" doc)
+                                    (show writeConcernErr) : errorsWithFailureIndex
+          (Just unknownValue, Nothing) -> do
+            return $ Left $ ProtocolFailure prevCount $ "Expected array of errors. Received: " ++ show unknownValue
+          (Just unknownValue, Just writeConcernErr) -> do
+            return $ Left $ CompoundFailure [ ProtocolFailure prevCount $ "Expected array of errors. Received: " ++ show unknownValue
+                                              , WriteFailure prevCount (fromMaybe 0 $ lookup "ok" doc) $ show writeConcernErr]
+
+splitAtLimit :: Int -> Int -> [Document] -> [Either Failure [Document]]
+splitAtLimit maxSize maxCount list = chop (go 0 0 []) list
+  where
+    go :: Int -> Int -> [Document] -> [Document] -> (Either Failure [Document], [Document])
+    go _ _ res [] = (Right $ reverse res, [])
+    go curSize curCount res (x : xs) =
+      let size = sizeOfDocument x + 8
+       in {- 8 bytes =
+            1 byte: element type.
+            6 bytes: key name. |key| <= log (maxWriteBatchSize = 100000)
+            1 byte: \x00.
+            See https://bsonspec.org/spec.html
+          -}
+          if (curSize + size > maxSize) || (curCount + 1 > maxCount)
+            then
+              if curCount == 0
+                then (Left $ WriteFailure 0 0 "One document is too big for the message", xs)
+                else (Right $ reverse res, x : xs)
+            else go (curSize + size) (curCount + 1) (x : res) xs
+
+    chop :: ([a] -> (b, [a])) -> [a] -> [b]
+    chop _ [] = []
+    chop f as = let (b, as') = f as in b : chop f as'
+
+sizeOfDocument :: Document -> Int
+sizeOfDocument d = fromIntegral $ LBS.length $ runPut $ putDocument d
+
+assignId :: Document -> IO Document
+-- ^ Assign a unique value to _id field if missing
+assignId doc = if any (("_id" ==) . label) doc
+    then return doc
+    else (\oid -> ("_id" =: oid) : doc) <$> genObjectId
+
+-- ** Update
+
+save :: (MonadIO m)
+     => Collection -> Document -> Action m ()
+-- ^ Save document to collection, meaning insert it if its new (has no \"_id\" field) or upsert it if its not new (has \"_id\" field)
+save col doc = case look "_id" doc of
+    Nothing -> insert_ col doc
+    Just i -> upsert (Select ["_id" := i] col) doc
+
+replace :: (MonadIO m)
+        => Selection -> Document -> Action m ()
+-- ^ Replace first document in selection with given document
+replace = update []
+
+repsert :: (MonadIO m)
+        => Selection -> Document -> Action m ()
+-- ^ Replace first document in selection with given document, or insert document if selection is empty
+repsert = update [Upsert]
+{-# DEPRECATED repsert "use upsert instead" #-}
+
+upsert :: (MonadIO m)
+       => Selection -> Document -> Action m ()
+-- ^ Update first document in selection with given document, or insert document if selection is empty
+upsert = update [Upsert]
+
+type Modifier = Document
+-- ^ Update operations on fields in a document. See <https://docs.mongodb.com/manual/reference/operator/update/>
+
+modify :: (MonadIO m)
+       => Selection -> Modifier -> Action m ()
+-- ^ Update all documents in selection using given modifier
+modify = update [MultiUpdate]
+
+update :: (MonadIO m)
+       => [UpdateOption] -> Selection -> Document -> Action m ()
+-- ^ 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.
+update opts (Select sel col) up = do
+    pipe <- asks mongoPipe
+    db <- thisDatabase
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then do
+        ctx <- ask
+        liftIO $ runReaderT (void $ write (Update (db <.> col) opts sel up)) ctx
+      else do
+        liftIOE ConnectionFailure $
+          P.sendOpMsg
+            pipe
+            [Nc (Update (db <.> col) opts sel up)]
+            (Just P.MoreToCome)
+            ["writeConcern" =: ["w" =: (0 :: Int32)]]
+
+updateCommandDocument :: Collection -> Bool -> [Document] -> Document -> Document
+updateCommandDocument col ordered updates writeConcern =
+  [ "update"  =: col
+  , "ordered" =: ordered
+  , "updates" =: updates
+  , "writeConcern" =: writeConcern
+  ]
+
+{-| Bulk update operation. If one update fails it will not update the remaining
+    documents. Current returned value is only a place holder. With mongodb server
+    before 2.6 it will send update requests one by one. In order to receive
+    error messages in versions under 2.6 you need to user confirmed writes.
+    Otherwise even if the errors had place the list of errors will be empty and
+    the result will be success. After 2.6 it will use bulk update feature in
+    mongodb.
+ -}
+updateMany :: (MonadIO m)
+           => Collection
+           -> [(Selector, Document, [UpdateOption])]
+           -> Action m WriteResult
+updateMany = update' True
+
+{-| Bulk update operation. If one update fails it will proceed with the
+    remaining documents. With mongodb server before 2.6 it will send update
+    requests one by one. In order to receive error messages in versions under
+    2.6 you need to use confirmed writes.  Otherwise even if the errors had
+    place the list of errors will be empty and the result will be success.
+    After 2.6 it will use bulk update feature in mongodb.
+ -}
+updateAll :: (MonadIO m)
+           => Collection
+           -> [(Selector, Document, [UpdateOption])]
+           -> Action m WriteResult
+updateAll = update' False
+
+update' :: (MonadIO m)
+        => Bool
+        -> Collection
+        -> [(Selector, Document, [UpdateOption])]
+        -> Action m WriteResult
+update' ordered col updateDocs = do
+  p <- asks mongoPipe
+  let sd = P.serverData p
+  let updates = map (\(s, d, os) -> [ "q" =: s
+                                    , "u" =: d
+                                    , "upsert" =: (Upsert `elem` os)
+                                    , "multi" =: (MultiUpdate `elem` os)])
+                updateDocs
+
+  mode <- asks mongoWriteMode
+  ctx <- ask
+  liftIO $ do
+    let writeConcern = case mode of
+                          NoConfirm -> ["w" =: (0 :: Int32)]
+                          Confirm params -> params
+    let docSize = sizeOfDocument $ updateCommandDocument
+                                                      col
+                                                      ordered
+                                                      []
+                                                      writeConcern
+    let preChunks = splitAtLimit
+                        (maxBsonObjectSize sd - docSize)
+                                             -- size of auxiliary part of update
+                                             -- document should be subtracted from
+                                             -- the overall size
+                        (maxWriteBatchSize sd)
+                        updates
+    let chunks =
+          if ordered
+              then takeRightsUpToLeft preChunks
+              else rights preChunks
+    let lens = map length chunks
+    let lSums = 0 : zipWith (+) lSums lens
+    blocks <- interruptibleFor ordered (zip lSums chunks) $ \b -> do
+      runReaderT (updateBlock ordered col b) ctx
+      `catch` \(e :: Failure) -> do
+        return $ WriteResult True 0 Nothing 0 [] [e] []
+    let failedTotal = any failed blocks
+    let updatedTotal = sum $ map nMatched blocks
+    let modifiedTotal =
+          if all (isNothing . nModified) blocks
+            then Nothing
+            else Just $ sum $ mapMaybe nModified blocks
+    let totalWriteErrors = concatMap writeErrors blocks
+    let totalWriteConcernErrors = concatMap writeConcernErrors blocks
+
+    let upsertedTotal = concatMap upserted blocks
+    return $ WriteResult
+                  failedTotal
+                  updatedTotal
+                  modifiedTotal
+                  0 -- nRemoved
+                  upsertedTotal
+                  totalWriteErrors
+                  totalWriteConcernErrors
+
+    `catch` \(e :: Failure) -> return $ WriteResult True 0 Nothing 0 [] [e] []
+
+updateBlock :: (MonadIO m)
+            => Bool -> Collection -> (Int, [Document]) -> Action m WriteResult
+updateBlock ordered col (prevCount, docs) = do
+  p <- asks mongoPipe
+  let sd = P.serverData p
+  if maxWireVersion sd < 2
+    then liftIO $ ioError $ userError "updateMany doesn't support mongodb older than 2.6"
+    else if maxWireVersion sd == 2 && maxWireVersion sd < 17 then do
+      mode <- asks mongoWriteMode
+      let writeConcern = case mode of
+                          NoConfirm -> ["w" =: (0 :: Int32)]
+                          Confirm params -> params
+      doc <- runCommand $ updateCommandDocument col ordered docs writeConcern
+
+      let n = fromMaybe 0 $ doc !? "n"
+      let writeErrorsResults =
+            case look "writeErrors" doc of
+              Nothing -> WriteResult False 0 (Just 0) 0 [] [] []
+              Just (Array err) -> WriteResult True 0 (Just 0) 0 [] (map (anyToWriteError prevCount) err) []
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      (Just 0)
+                                      0
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected array of error docs, but received: "
+                                              ++ show unknownErr]
+                                      []
+
+      let writeConcernResults =
+            case look "writeConcernError" doc of
+              Nothing ->  WriteResult False 0 (Just 0) 0 [] [] []
+              Just (Doc err) -> WriteResult
+                                    True
+                                    0
+                                    (Just 0)
+                                    0
+                                    []
+                                    []
+                                    [ WriteConcernFailure
+                                        (fromMaybe (-1) $ err !? "code")
+                                        (fromMaybe "" $ err !? "errmsg")
+                                    ]
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      (Just 0)
+                                      0
+                                      []
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected doc in writeConcernError, but received: "
+                                              ++ show unknownErr]
+
+      let upsertedList = maybe [] (map docToUpserted) (doc !? "upserted")
+      let successResults = WriteResult False n (doc !? "nModified") 0 upsertedList [] []
+      return $ foldl1' mergeWriteResults [writeErrorsResults, writeConcernResults, successResults]
+    else do
+      mode <- asks mongoWriteMode
+      let writeConcern = case mode of
+                          NoConfirm -> ["w" =: (0 :: Int32)]
+                          Confirm params -> merge params ["w" =: (1 :: Int32)]
+      doc <- runCommand $ updateCommandDocument col ordered docs writeConcern
+
+      let n = fromMaybe 0 $ doc !? "n"
+      let writeErrorsResults =
+            case look "writeErrors" doc of
+              Nothing -> WriteResult False 0 (Just 0) 0 [] [] []
+              Just (Array err) -> WriteResult True 0 (Just 0) 0 [] (map (anyToWriteError prevCount) err) []
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      (Just 0)
+                                      0
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected array of error docs, but received: "
+                                              ++ show unknownErr]
+                                      []
+
+      let writeConcernResults =
+            case look "writeConcernError" doc of
+              Nothing ->  WriteResult False 0 (Just 0) 0 [] [] []
+              Just (Doc err) -> WriteResult
+                                    True
+                                    0
+                                    (Just 0)
+                                    0
+                                    []
+                                    []
+                                    [ WriteConcernFailure
+                                        (fromMaybe (-1) $ err !? "code")
+                                        (fromMaybe "" $ err !? "errmsg")
+                                    ]
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      (Just 0)
+                                      0
+                                      []
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected doc in writeConcernError, but received: "
+                                              ++ show unknownErr]
+
+      let upsertedList = maybe [] (map docToUpserted) (doc !? "upserted")
+      let successResults = WriteResult False n (doc !? "nModified") 0 upsertedList [] []
+      return $ foldl1' mergeWriteResults [writeErrorsResults, writeConcernResults, successResults]
+
+interruptibleFor :: (Monad m, Result b) => Bool -> [a] -> (a -> m b) -> m [b]
+interruptibleFor ordered = go []
+  where
+    go !res [] _ = return $ reverse res
+    go !res (x:xs) f = do
+      y <- f x
+      if isFailed y && ordered
+        then return $ reverse (y:res)
+        else go (y:res) xs f
+
+mergeWriteResults :: WriteResult -> WriteResult -> WriteResult
+mergeWriteResults
+  (WriteResult failed1 nMatched1 nModified1 nDeleted1 upserted1 writeErrors1 writeConcernErrors1)
+  (WriteResult failed2 nMatched2 nModified2 nDeleted2 upserted2 writeErrors2 writeConcernErrors2) =
+    WriteResult
+        (failed1 || failed2)
+        (nMatched1 + nMatched2)
+        (liftM2 (+) nModified1 nModified2)
+        (nDeleted1 + nDeleted2)
+        -- This function is used in foldl1' function. The first argument is the accumulator.
+        -- The list in the accumulator is usually longer than the subsequent value which goes in the second argument.
+        -- So, changing the order of list concatenation allows us to keep linear complexity of the
+        -- whole list accumulation process.
+        (upserted2 ++ upserted1)
+        (writeErrors2 ++ writeErrors1)
+        (writeConcernErrors2 ++ writeConcernErrors1)
+
+
+docToUpserted :: Document -> Upserted
+docToUpserted doc = Upserted ind uid
+  where
+    ind = at "index" doc
+    uid = at "_id"   doc
+
+docToWriteError :: Document -> Failure
+docToWriteError doc = WriteFailure ind code msg
+  where
+    ind  = at "index"  doc
+    code = at "code"   doc
+    msg  = at "errmsg" doc
+
+-- ** Delete
+
+delete :: (MonadIO m)
+       => Selection -> Action m ()
+-- ^ Delete all documents in selection
+delete s = do
+    pipe <- asks mongoPipe
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then deleteHelper [] s
+      else deleteMany (coll s) [([], [])] >> return ()
+
+deleteOne :: (MonadIO m)
+          => Selection -> Action m ()
+-- ^ Delete first document in selection
+deleteOne sel@((Select sel' col)) = do
+    pipe <- asks mongoPipe
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then deleteHelper [SingleRemove] sel
+      else do
+        -- Starting with v6 confirming writes via getLastError as it is
+        -- performed in the deleteHelper call via its call to write is
+        -- deprecated. To confirm writes now an appropriate writeConcern has to be
+        -- set. These confirmations were discarded in deleteHelper anyway so no
+        -- need to dispatch on the writeConcern as it is currently done in deleteHelper
+        -- via write for older versions
+        db <- thisDatabase
+        liftIOE ConnectionFailure $
+          P.sendOpMsg
+            pipe
+            [Nc (Delete (db <.> col) [] sel')]
+            (Just P.MoreToCome)
+            ["writeConcern" =: ["w" =: (0 :: Int32)]]
+
+deleteHelper :: (MonadIO m)
+             => [DeleteOption] -> Selection -> Action m ()
+deleteHelper opts (Select sel col) = do
+    ctx <- ask
+    db <- thisDatabase
+    liftIO $ runReaderT (void $ write (Delete (db <.> col) opts sel)) ctx
+
+{-| Bulk delete operation. If one delete fails it will not delete the remaining
+    documents. Current returned value is only a place holder. With mongodb server
+    before 2.6 it will send delete requests one by one. After 2.6 it will use
+    bulk delete feature in mongodb.
+ -}
+deleteMany :: (MonadIO m)
+           => Collection
+           -> [(Selector, [DeleteOption])]
+           -> Action m WriteResult
+deleteMany = delete' True
+
+{-| Bulk delete operation. If one delete fails it will proceed with the
+    remaining documents. Current returned value is only a place holder. With
+    mongodb server before 2.6 it will send delete requests one by one. After 2.6
+    it will use bulk delete feature in mongodb.
+ -}
+deleteAll :: (MonadIO m)
+          => Collection
+          -> [(Selector, [DeleteOption])]
+          -> Action m WriteResult
+deleteAll = delete' False
+
+deleteCommandDocument :: Collection -> Bool -> [Document] -> Document -> Document
+deleteCommandDocument col ordered deletes writeConcern =
+  [ "delete"       =: col
+  , "ordered"      =: ordered
+  , "deletes"      =: deletes
+  , "writeConcern" =: writeConcern
+  ]
+
+delete' :: (MonadIO m)
+        => Bool
+        -> Collection
+        -> [(Selector, [DeleteOption])]
+        -> Action m WriteResult
+delete' ordered col deleteDocs = do
+  p <- asks mongoPipe
+  let sd = P.serverData p
+  let deletes = map (\(s, os) -> [ "q"     =: s
+                                 , "limit" =: if SingleRemove `elem` os
+                                               then (1 :: Int) -- Remove only one matching
+                                               else (0 :: Int) -- Remove all matching
+                                 ])
+                    deleteDocs
+
+  mode <- asks mongoWriteMode
+  let writeConcern = case mode of
+                        NoConfirm -> ["w" =: (0 :: Int32)]
+                        Confirm params -> params
+  let docSize = sizeOfDocument $ deleteCommandDocument col ordered [] writeConcern
+  let chunks = splitAtLimit
+                      (maxBsonObjectSize sd - docSize)
+                                           -- size of auxiliary part of delete
+                                           -- document should be subtracted from
+                                           -- the overall size
+                      (maxWriteBatchSize sd)
+                      deletes
+  ctx <- ask
+  let lens = map (either (const 1) length) chunks
+  let lSums = 0 : zipWith (+) lSums lens
+  let failureResult e = return $ WriteResult True 0 Nothing 0 [] [e] []
+  let doChunk b = runReaderT (deleteBlock ordered col b) ctx `catch` failureResult
+  blockResult <- liftIO $ interruptibleFor ordered (zip lSums chunks) $ \(n, c) ->
+    case c of
+      Left e -> failureResult e
+      Right b -> doChunk (n, b)
+  return $ foldl1' mergeWriteResults blockResult
+
+
+addFailureIndex :: Int -> Failure -> Failure
+addFailureIndex i (WriteFailure ind code s) = WriteFailure (ind + i) code s
+addFailureIndex _ f = f
+
+deleteBlock :: (MonadIO m)
+            => Bool -> Collection -> (Int, [Document]) -> Action m WriteResult
+deleteBlock ordered col (prevCount, docs) = do
+  p <- asks mongoPipe
+  let sd = P.serverData p
+  if maxWireVersion sd < 2
+    then liftIO $ ioError $ userError "deleteMany doesn't support mongodb older than 2.6"
+    else if maxWireVersion sd == 2 && maxWireVersion sd < 17 then do
+      mode <- asks mongoWriteMode
+      let writeConcern = case mode of
+                          NoConfirm -> ["w" =: (0 :: Int32)]
+                          Confirm params -> params
+      doc <- runCommand $ deleteCommandDocument col ordered docs writeConcern
+      let n = fromMaybe 0 $ doc !? "n"
+
+      let successResults = WriteResult False 0 Nothing n [] [] []
+      let writeErrorsResults =
+            case look "writeErrors" doc of
+              Nothing ->  WriteResult False 0 Nothing 0 [] [] []
+              Just (Array err) -> WriteResult True 0 Nothing 0 [] (map (anyToWriteError prevCount) err) []
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      Nothing
+                                      0
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected array of error docs, but received: "
+                                              ++ show unknownErr]
+                                      []
+      let writeConcernResults =
+            case look "writeConcernError" doc of
+              Nothing ->  WriteResult False 0 Nothing 0 [] [] []
+              Just (Doc err) -> WriteResult
+                                    True
+                                    0
+                                    Nothing
+                                    0
+                                    []
+                                    []
+                                    [ WriteConcernFailure
+                                        (fromMaybe (-1) $ err !? "code")
+                                        (fromMaybe "" $ err !? "errmsg")
+                                    ]
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      Nothing
+                                      0
+                                      []
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected doc in writeConcernError, but received: "
+                                              ++ show unknownErr]
+      return $ foldl1' mergeWriteResults [successResults, writeErrorsResults, writeConcernResults]
+    else do
+      mode <- asks mongoWriteMode
+      let writeConcern = case mode of
+                          NoConfirm -> ["w" =: (0 :: Int32)]
+                          Confirm params -> merge params ["w" =: (1 :: Int32)]
+      doc <- runCommand $ deleteCommandDocument col ordered docs writeConcern
+      let n = fromMaybe 0 $ doc !? "n"
+
+      let successResults = WriteResult False 0 Nothing n [] [] []
+      let writeErrorsResults =
+            case look "writeErrors" doc of
+              Nothing ->  WriteResult False 0 Nothing 0 [] [] []
+              Just (Array err) -> WriteResult True 0 Nothing 0 [] (map (anyToWriteError prevCount) err) []
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      Nothing
+                                      0
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected array of error docs, but received: "
+                                              ++ show unknownErr]
+                                      []
+      let writeConcernResults =
+            case look "writeConcernError" doc of
+              Nothing ->  WriteResult False 0 Nothing 0 [] [] []
+              Just (Doc err) -> WriteResult
+                                    True
+                                    0
+                                    Nothing
+                                    0
+                                    []
+                                    []
+                                    [ WriteConcernFailure
+                                        (fromMaybe (-1) $ err !? "code")
+                                        (fromMaybe "" $ err !? "errmsg")
+                                    ]
+              Just unknownErr -> WriteResult
+                                      True
+                                      0
+                                      Nothing
+                                      0
+                                      []
+                                      []
+                                      [ ProtocolFailure
+                                            prevCount
+                                          $ "Expected doc in writeConcernError, but received: "
+                                              ++ show unknownErr]
+      return $ foldl1' mergeWriteResults [successResults, writeErrorsResults, writeConcernResults]
+
+anyToWriteError :: Int -> Value -> Failure
+anyToWriteError _ (Doc d) = docToWriteError d
+anyToWriteError ind _ = ProtocolFailure ind "Unknown bson value"
+
+-- * Read
+
+data ReadMode =
+      Fresh  -- ^ read from master only
+    | StaleOk  -- ^ read from slave ok
+    deriving (Show, Eq)
+
+readModeOption :: ReadMode -> [QueryOption]
+readModeOption Fresh = []
+readModeOption StaleOk = [SlaveOK]
+
+-- ** Query
+
+-- | Use 'select' to create a basic query with defaults, then modify if desired. For example, @(select sel col) {limit = 10}@
+data Query = Query {
+    options :: [QueryOption],  -- ^ Default = @[]@
+    selection :: Selection,
+    project :: Projector,  -- ^ @[]@ = all fields. Default = @[]@
+    skip :: Word32,  -- ^ Number of initial matching documents to skip. Default = 0
+    limit :: Limit, -- ^ Maximum number of documents to return, 0 = no limit. Default = 0
+    sort :: Order,  -- ^ Sort results by this order, @[]@ = no sort. Default = @[]@
+    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. Default = @False@
+    batchSize :: BatchSize,  -- ^ The number of document to return in each batch response from the server. 0 means use Mongo default. Default = 0
+    hint :: Order  -- ^ Force MongoDB to use this index, @[]@ = no hint. Default = @[]@
+    } deriving (Show, Eq)
+
+type Projector = 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@.
+
+type Limit = Word32
+-- ^ Maximum number of documents to return, i.e. cursor will close after iterating over this number of documents. 0 means no limit.
+
+type Order = 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
+
+type BatchSize = Word32
+-- ^ The number of document to return in each batch response from the server. 0 means use Mongo default.
+
+-- noticeCommands and adminCommands are needed to identify whether
+-- queryRequestOpMsg is called via runCommand or not. If not it will
+-- behave like being called by a "find"-like command and add additional fields
+-- specific to the find command into the selector, such as "filter", "projection" etc.
+noticeCommands :: [Text]
+noticeCommands = [ "aggregate"
+                 , "count"
+                 , "delete"
+                 , "findAndModify"
+                 , "insert"
+                 , "listCollections"
+                 , "update"
+                 ]
+
+adminCommands :: [Text]
+adminCommands = [ "buildinfo"
+                , "clone"
+                , "collstats"
+                , "copydb"
+                , "copydbgetnonce"
+                , "create"
+                , "dbstats"
+                , "deleteIndexes"
+                , "drop"
+                , "dropDatabase"
+                , "renameCollection"
+                , "repairDatabase"
+                , "serverStatus"
+                , "validate"
+                ]
+
+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.
+query sel col = Query [] (Select sel col) [] 0 0 [] False 0 []
+
+find :: MonadIO m => Query -> Action m Cursor
+-- ^ Fetch documents satisfying query
+find q@Query{selection, batchSize} = do
+    pipe <- asks mongoPipe
+    db <- thisDatabase
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then do
+        qr <- queryRequest False q
+        dBatch <- liftIO $ request pipe [] qr
+        newCursor db (coll selection) batchSize dBatch
+      else do
+        qr <- queryRequestOpMsg False q
+        let newQr =
+              case fst qr of
+                Req P.Query{..} ->
+                  let coll = last $ T.splitOn "." qFullCollection
+                  in (Req $ P.Query {qSelector = merge qSelector [ "find" =: coll ], ..}, snd qr)
+                -- queryRequestOpMsg only returns Cmd types constructed via Req
+                _ -> error "impossible"
+        dBatch <- liftIO $ requestOpMsg pipe newQr []
+        newCursor db (coll selection) batchSize dBatch
+
+findCommand :: (MonadIO m) => Query -> Action m Cursor
+-- ^ Fetch documents satisfying query using the command "find"
+findCommand q@Query{..} = do
+    pipe <- asks mongoPipe
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then do
+        let aColl = coll selection
+        response <- runCommand $
+          [ "find"        =: aColl
+          , "filter"      =: selector selection
+          , "sort"        =: sort
+          , "projection"  =: project
+          , "hint"        =: hint
+          , "skip"        =: toInt32 skip
+          ]
+          ++ mconcat -- optional fields. They should not be present if set to 0 and mongo will use defaults
+             [ "batchSize" =? toMaybe (/= 0) toInt32 batchSize
+             , "limit"     =? toMaybe (/= 0) toInt32 limit
+             ]
+        getCursorFromResponse aColl response
+          >>= either (liftIO . throwIO . QueryFailure (at "code" response)) return
+      else find q
+    where
+      toInt32 :: Integral a => a -> Int32
+      toInt32 = fromIntegral
+
+      toMaybe :: (a -> Bool) -> (a -> b) -> a -> Maybe b
+      toMaybe predicate f a
+        | predicate a = Just (f a)
+        | otherwise   = Nothing
+
+isHandshake :: Document -> Bool
+isHandshake = (== ["isMaster" =: (1 :: Int32)])
+
+findOne :: (MonadIO m) => Query -> Action m (Maybe Document)
+-- ^ Fetch first document satisfying query or @Nothing@ if none satisfy it
+findOne q = do
+    pipe <- asks mongoPipe
+    let legacyQuery = do
+            qr <- queryRequest False q {limit = 1}
+            rq <- liftIO $ request pipe [] qr
+            Batch _ _ docs <- liftDB $ fulfill rq
+            return (listToMaybe docs)
+    if isHandshake (selector $ selection q)
+      then legacyQuery
+      else do
+        let sd = P.serverData pipe
+        if (maxWireVersion sd < 17)
+          then legacyQuery
+          else do
+            qr <- queryRequestOpMsg False q {limit = 1}
+            let newQr =
+                  case fst qr of
+                    Req P.Query{..} ->
+                      let coll = last $ T.splitOn "." qFullCollection
+                          -- We have to understand whether findOne is called as
+                          -- command directly. This is necessary since findOne is used via
+                          -- runCommand as a vehicle to execute any type of commands and notices.
+                          labels = catMaybes $ map (\f -> look f qSelector) (noticeCommands ++ adminCommands) :: [Value]
+                      in if null labels
+                           then (Req P.Query {qSelector = merge qSelector [ "find" =: coll ], ..}, snd qr)
+                           else qr
+                    _ -> error "impossible"
+            rq <- liftIO $ requestOpMsg pipe newQr []
+            Batch _ _ docs <- liftDB $ fulfill rq
+            return (listToMaybe docs)
+
+fetch :: (MonadIO m) => Query -> Action m Document
+-- ^ Same as 'findOne' except throw 'DocNotFound' if none match
+fetch q = findOne q >>= maybe (liftIO $ throwIO $ DocNotFound $ selection q) return
+
+-- | Options for @findAndModify@
+data FindAndModifyOpts
+  = FamRemove Bool -- ^ remove the selected document when the boolean is @True@
+  | FamUpdate
+    { famUpdate :: Document -- ^ the update instructions, or a replacement document
+    , famNew :: Bool -- ^ return the document with the modifications made on the update
+    , famUpsert :: Bool -- ^ create a new document if no documents match the query
+    }
+  deriving Show
+
+-- | Default options used by 'findAndModify'.
+defFamUpdateOpts :: Document -> FindAndModifyOpts
+defFamUpdateOpts ups = FamUpdate
+                       { famNew = True
+                       , famUpsert = False
+                       , famUpdate = ups
+                       }
+
+-- | Run the @findAndModify@ command as an update without an upsert and new set to @True@.
+-- Return a single updated document (@new@ option is set to @True@).
+--
+-- See 'findAndModifyOpts' for more options.
+findAndModify :: (MonadIO m)
+              => Query
+              -> Document -- ^ updates
+              -> Action m (Either String Document)
+findAndModify q ups = do
+  eres <- findAndModifyOpts q (defFamUpdateOpts ups)
+  return $ case eres of
+    Left l -> Left l
+    Right r -> case r of
+      -- only possible when upsert is True and new is False
+      Nothing  -> Left "findAndModify: impossible null result"
+      Just doc -> Right doc
+
+-- | Run the @findAndModify@ command
+-- (allows more options than 'findAndModify')
+findAndModifyOpts :: (MonadIO m)
+                  => Query
+                  -> FindAndModifyOpts
+                  -> Action m (Either String (Maybe Document))
+findAndModifyOpts Query {
+    selection = Select sel collection
+  , project = project
+  , sort = sort
+  } famOpts = do
+    result <- runCommand
+       ([ "findAndModify" := String collection
+        , "query"  := Doc sel
+        , "fields" := Doc project
+        , "sort"   := Doc sort
+        ] ++
+            case famOpts of
+              FamRemove shouldRemove -> [ "remove" := Bool shouldRemove ]
+              FamUpdate {..} ->
+                [ "update" := Doc famUpdate
+                , "new"    := Bool famNew    -- return updated document, not original document
+                , "upsert" := Bool famUpsert -- insert if nothing is found
+                ])
+    return $ case lookupErr result of
+        Just e -> leftErr e
+        Nothing -> case lookup "value" result of
+            Nothing   -> leftErr "no document found"
+            Just mdoc -> case mdoc of
+                Just doc@(_:_) -> Right (Just doc)
+                Just [] -> case famOpts of
+                    FamUpdate { famUpsert = True, famNew = False } -> Right Nothing
+                    _ -> leftErr $ show result
+                _  -> leftErr $ show result
+  where
+    leftErr err = Left $ "findAndModify " `mappend` show collection
+        `mappend` "\nfrom query: " `mappend` show sel
+        `mappend` "\nerror: " `mappend` err
+
+    -- return Nothing means ok, Just is the error message
+    lookupErr :: Document -> Maybe String
+    lookupErr result = do
+        errObject <- lookup "lastErrorObject" result
+        lookup "err" errObject
+
+explain :: (MonadIO m) => Query -> Action m Document
+-- ^ Return performance stats of query execution
+explain q = do  -- same as findOne but with explain set to true
+    pipe <- asks mongoPipe
+    qr <- queryRequest True q {limit = 1}
+    r <- liftIO $ request pipe [] qr
+    Batch _ _ docs <- liftDB $ fulfill r
+    return $ if null docs then error ("no explain: " ++ show q) else head docs
+
+count :: (MonadIO m) => Query -> Action m Int
+-- ^ Fetch number of documents satisfying query (including effect of skip and/or limit if present)
+count Query{selection = Select sel col, skip, limit} = at "n" <$> runCommand
+    (["count" =: col, "query" =: sel, "skip" =: (fromIntegral skip :: Int32)]
+        ++ ("limit" =? if limit == 0 then Nothing else Just (fromIntegral limit :: Int32)))
+
+distinct :: (MonadIO m) => Label -> Selection -> Action m [Value]
+-- ^ Fetch distinct values of field in selected documents
+distinct k (Select sel col) = at "values" <$> runCommand ["distinct" =: col, "key" =: k, "query" =: sel]
+
+queryRequest :: (Monad m, MonadIO m) => Bool -> Query -> Action m (Request, Maybe Limit)
+-- ^ Translate Query to Protocol.Query. If first arg is true then add special $explain attribute.
+queryRequest isExplain Query{..} = do
+    ctx <- ask
+    return $ queryRequest' (mongoReadMode ctx) (mongoDatabase ctx)
+ where
+    queryRequest' rm db = (P.Query{..}, remainingLimit) where
+        qOptions = readModeOption rm ++ options
+        qFullCollection = db <.> coll selection
+        qSkip = fromIntegral skip
+        (qBatchSize, remainingLimit) = batchSizeRemainingLimit batchSize (if limit == 0 then Nothing else Just limit)
+        qProjector = project
+        mOrder = if null sort then Nothing else Just ("$orderby" =: sort)
+        mSnapshot = if snapshot then Just ("$snapshot" =: True) else Nothing
+        mHint = if null hint then Nothing else Just ("$hint" =: hint)
+        mExplain = if isExplain then Just ("$explain" =: True) else Nothing
+        special = catMaybes [mOrder, mSnapshot, mHint, mExplain]
+        qSelector = if null special then s else ("$query" =: s) : special where s = selector selection
+
+queryRequestOpMsg :: (Monad m, MonadIO m) => Bool -> Query -> Action m (Cmd, Maybe Limit)
+-- ^ Translate Query to Protocol.Query. If first arg is true then add special $explain attribute.
+queryRequestOpMsg isExplain Query{..} = do
+    ctx <- ask
+    return $ queryRequest' (mongoReadMode ctx) (mongoDatabase ctx)
+ where
+    queryRequest' rm db = (Req P.Query{..}, remainingLimit) where
+        qOptions = readModeOption rm ++ options
+        qFullCollection = db <.> coll selection
+        qSkip = fromIntegral skip
+        (qBatchSize, remainingLimit) = batchSizeRemainingLimit batchSize (if limit == 0 then Nothing else Just limit)
+        -- Check whether this query is not a command in disguise. If
+        -- isNotCommand is true, then we treat this as a find command and add
+        -- the relevant fields to the selector
+        isNotCommand = null $ catMaybes $ map (\l -> look l (selector selection)) (noticeCommands ++ adminCommands)
+        mOrder = if null sort then Nothing else Just ("sort" =: sort)
+        mSnapshot = if snapshot then Just ("snapshot" =: True) else Nothing
+        mHint = if null hint then Nothing else Just ("hint" =: hint)
+        mExplain = if isExplain then Just ("$explain" =: True) else Nothing
+        special = catMaybes [mOrder, mSnapshot, mHint, mExplain]
+        qProjector = if isNotCommand then ["projection" =: project] else project
+        qSelector = if isNotCommand then c else s
+          where s = selector selection
+                bSize = if qBatchSize == 0 then Nothing else Just ("batchSize" =: qBatchSize)
+                mLimit = if limit == 0 then Nothing else maybe Nothing (\rL -> Just ("limit" =: (fromIntegral rL :: Int32))) remainingLimit
+                c = ("filter" =: s) : special ++ maybeToList bSize ++ maybeToList mLimit
+
+batchSizeRemainingLimit :: BatchSize -> Maybe Limit -> (Int32, Maybe Limit)
+-- ^ Given batchSize and limit return P.qBatchSize and remaining limit
+batchSizeRemainingLimit batchSize mLimit =
+  let remaining =
+        case mLimit of
+          Nothing    -> batchSize
+          Just limit ->
+            if 0 < batchSize && batchSize < limit
+              then batchSize
+              else limit
+  in (fromIntegral remaining, mLimit)
+
+type DelayedBatch = IO Batch
+-- ^ A promised batch which may fail
+
+data Batch = Batch (Maybe Limit) CursorId [Document]
+-- ^ CursorId = 0 means cursor is finished. Documents is remaining documents to serve in current batch. Limit is number of documents to return. Nothing means no limit.
+
+request :: Pipe -> [Notice] -> (Request, Maybe Limit) -> IO DelayedBatch
+-- ^ Send notices and request and return promised batch
+request pipe ns (req, remainingLimit) = do
+    promise <- liftIOE ConnectionFailure $ P.call pipe ns req
+    let protectedPromise = liftIOE ConnectionFailure promise
+    return $ fromReply remainingLimit =<< protectedPromise
+
+requestOpMsg :: Pipe -> (Cmd, Maybe Limit) -> Document -> IO DelayedBatch
+-- ^ Send notices and request and return promised batch
+requestOpMsg pipe (Req r, remainingLimit) params = do
+  promise <- liftIOE ConnectionFailure $ P.callOpMsg pipe r Nothing params
+  let protectedPromise = liftIOE ConnectionFailure promise
+  return $ fromReply remainingLimit =<< protectedPromise
+requestOpMsg _ _ _ = error "requestOpMsg: Only messages of type Query are supported"
+
+fromReply :: Maybe Limit -> Reply -> DelayedBatch
+-- ^ Convert Reply to Batch or Failure
+fromReply limit Reply{..} = do
+    mapM_ checkResponseFlag rResponseFlags
+    return (Batch limit rCursorId rDocuments)
+ where
+    -- If response flag indicates failure then throw it, otherwise do nothing
+    checkResponseFlag flag = case flag of
+        AwaitCapable -> return ()
+        CursorNotFound -> throwIO $ CursorNotFoundFailure rCursorId
+        QueryError -> throwIO $ QueryFailure (at "code" $ head rDocuments) (at "$err" $ head rDocuments)
+fromReply limit ReplyOpMsg{..} = do
+    let section = head sections
+        cur = maybe Nothing cast $ look "cursor" section
+    case cur of
+      Nothing -> return (Batch limit 0 sections)
+      Just doc ->
+          case look "firstBatch" doc of
+            Just ar -> do
+              let docs = fromJust $ cast ar
+                  id' = fromJust $ cast $ valueAt "id" doc
+              return (Batch limit id' docs)
+            -- A cursor without a firstBatch field, should be a reply to a
+            -- getMore query and thus have a nextBatch key
+            Nothing -> do
+              let docs = fromJust $ cast $ valueAt "nextBatch" doc
+                  id' = fromJust $ cast $ valueAt "id" doc
+              return (Batch limit id' docs)
+
+fulfill :: DelayedBatch -> Action IO Batch
+-- ^ Demand and wait for result, raise failure if exception
+fulfill = liftIO
+
+-- *** Cursor
+
+data Cursor = Cursor FullCollection BatchSize (MVar DelayedBatch)
+-- ^ 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 'CursorNotFoundFailure'. Note, a cursor is not closed when the pipe is closed, so you can open another pipe to the same server and continue using the cursor.
+
+newCursor :: MonadIO m => Database -> Collection -> BatchSize -> DelayedBatch -> Action m Cursor
+-- ^ Create new cursor. If you don't read all results then close it. Cursor will be closed automatically when all results are read from it or when eventually garbage collected.
+newCursor db col batchSize dBatch = do
+    var <- liftIO $ MV.newMVar dBatch
+    let cursor = Cursor (db <.> col) batchSize var
+    _ <- liftDB $ mkWeakMVar var (closeCursor cursor)
+    return cursor
+
+nextBatch :: MonadIO m => Cursor -> Action m [Document]
+-- ^ Return next batch of documents in query result, which will be empty if finished.
+nextBatch (Cursor fcol batchSize var) = liftDB $ modifyMVar var $ \dBatch -> do
+    -- Pre-fetch next batch promise from server and return current batch.
+    Batch mLimit cid docs <- liftDB $ fulfill' fcol batchSize dBatch
+    let newLimit = do
+              limit <- mLimit
+              return $ limit - min limit (fromIntegral $ length docs)
+    let emptyBatch = return $ Batch (Just 0) 0 []
+    let getNextBatch = nextBatch' fcol batchSize newLimit cid
+    let resultDocs = maybe id (take . fromIntegral) mLimit docs
+    case (cid, newLimit) of
+      (0, _)      -> return (emptyBatch, resultDocs)
+      (_, Just 0) -> do
+        pipe <- asks mongoPipe
+        liftIOE ConnectionFailure $ P.send pipe [KillCursors [cid]]
+        return (emptyBatch, resultDocs)
+      (_, _)      -> (, resultDocs) <$> getNextBatch
+
+fulfill' :: FullCollection -> BatchSize -> DelayedBatch -> Action IO Batch
+-- Discard pre-fetched batch if empty with nonzero cid.
+fulfill' fcol batchSize dBatch = do
+    b@(Batch limit cid docs) <- fulfill dBatch
+    if cid /= 0 && null docs && (limit > Just 0)
+        then nextBatch' fcol batchSize limit cid >>= fulfill
+        else return b
+
+nextBatch' :: (MonadIO m) => FullCollection -> BatchSize -> Maybe Limit -> CursorId -> Action m DelayedBatch
+nextBatch' fcol batchSize limit cid = do
+    pipe <- asks mongoPipe
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then liftIO $ request pipe [] (GetMore fcol batchSize' cid, remLimit)
+      else liftIO $ requestOpMsg pipe (Req $ GetMore fcol batchSize' cid, remLimit) []
+    where (batchSize', remLimit) = batchSizeRemainingLimit batchSize limit
+
+next :: MonadIO m => Cursor -> Action m (Maybe Document)
+-- ^ Return next document in query result, or Nothing if finished.
+next (Cursor fcol batchSize var) = liftDB $ modifyMVar var nextState where
+    -- Pre-fetch next batch promise from server when last one in current batch is returned.
+    -- nextState:: DelayedBatch -> Action m (DelayedBatch, Maybe Document)
+    nextState dBatch = do
+        Batch mLimit cid docs <- liftDB $ fulfill' fcol batchSize dBatch
+        if mLimit == Just 0
+          then return (return $ Batch (Just 0) 0 [], Nothing)
+          else
+            case docs of
+                doc : docs' -> do
+                    let newLimit = do
+                              limit <- mLimit
+                              return $ limit - 1
+                    dBatch' <- if null docs' && cid /= 0 && ((newLimit > Just 0) || isNothing newLimit)
+                        then nextBatch' fcol batchSize newLimit cid
+                        else return $ return (Batch newLimit cid docs')
+                    when (newLimit == Just 0) $ unless (cid == 0) $ do
+                      pipe <- asks mongoPipe
+                      let sd = P.serverData pipe
+                      if maxWireVersion sd < 17
+                        then liftIOE ConnectionFailure $ P.send pipe [KillCursors [cid]]
+                        else liftIOE ConnectionFailure $ P.sendOpMsg pipe [Kc (P.KillC (KillCursors [cid]) fcol)] (Just MoreToCome) []
+                    return (dBatch', Just doc)
+                [] -> if cid == 0
+                    then return (return $ Batch (Just 0) 0 [], Nothing)  -- finished
+                    else do
+                      nb <- nextBatch' fcol batchSize mLimit cid
+                      return (nb, Nothing)
+
+nextN :: MonadIO m => Int -> Cursor -> Action m [Document]
+-- ^ Return next N documents or less if end is reached
+nextN n c = catMaybes <$> replicateM n (next c)
+
+rest :: MonadIO m => Cursor -> Action m [Document]
+-- ^ Return remaining documents in query result
+rest c = loop (next c)
+
+closeCursor :: MonadIO m => Cursor -> Action m ()
+closeCursor (Cursor _ _ var) = liftDB $ modifyMVar var $ \dBatch -> do
+    Batch _ cid _ <- fulfill dBatch
+    unless (cid == 0) $ do
+      pipe <- asks mongoPipe
+      liftIOE ConnectionFailure $ P.send pipe [KillCursors [cid]]
+    return (return $ Batch (Just 0) 0 [], ())
+
+isCursorClosed :: MonadIO m => Cursor -> Action m Bool
+isCursorClosed (Cursor _ _ var) = do
+        Batch _ cid docs <- liftDB $ fulfill =<< readMVar var
+        return (cid == 0 && null docs)
+
+-- ** Aggregate
+
+type Pipeline = [Document]
+-- ^ The Aggregate Pipeline
+
+aggregate :: (MonadIO m) => Collection -> Pipeline -> Action m [Document]
+-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
+aggregate aColl agg = do
+    aggregateCursor aColl agg def >>= rest
+
+data AggregateConfig = AggregateConfig
+  { allowDiskUse :: Bool -- ^ Enable writing to temporary files (aggregations have a 100Mb RAM limit)
+  }
+  deriving Show
+
+instance Default AggregateConfig where
+  def = AggregateConfig
+    { allowDiskUse = False
+    }
+
+aggregateCommand :: Collection -> Pipeline -> AggregateConfig -> Document
+aggregateCommand aColl agg AggregateConfig {..} =
+  [ "aggregate" =: aColl
+  , "pipeline" =: agg
+  , "cursor" =: ([] :: Document)
+  , "allowDiskUse" =: allowDiskUse
+  ]
+
+aggregateCursor :: (MonadIO m) => Collection -> Pipeline -> AggregateConfig -> Action m Cursor
+-- ^ Runs an aggregate and unpacks the result. See <http://docs.mongodb.org/manual/core/aggregation/> for details.
+aggregateCursor aColl agg cfg = do
+    pipe <- asks mongoPipe
+    let sd = P.serverData pipe
+    if maxWireVersion sd < 17
+      then do
+        response <- runCommand (aggregateCommand aColl agg cfg)
+        getCursorFromResponse aColl response
+           >>= either (liftIO . throwIO . AggregateFailure) return
+      else do
+        let q = select (aggregateCommand aColl agg cfg) aColl
+        qr <- queryRequestOpMsg False q
+        dBatch <- liftIO $ requestOpMsg pipe qr []
+        db <- thisDatabase
+        Right <$> newCursor db aColl 0 dBatch
+           >>= either (liftIO . throwIO . AggregateFailure) return
+
+getCursorFromResponse
+  :: (MonadIO m)
+  => Collection
+  -> Document
+  -> Action m (Either String Cursor)
+getCursorFromResponse aColl response
+  | true1 "ok" response = runExceptT $ do
+      cursor     <- lookup "cursor" response ?? "cursor is missing"
+      firstBatch <- lookup "firstBatch" cursor ?? "firstBatch is missing"
+      cursorId   <- lookup "id" cursor ?? "id is missing"
+      db         <- lift thisDatabase
+      lift $ newCursor db aColl 0 (return $ Batch Nothing cursorId firstBatch)
+  | otherwise = return $ Left $ at "errmsg" response
+  where
+    Nothing ?? e = throwE e
+    Just a ?? _ = pure a
+
+-- ** Group
+
+-- | Groups documents in collection by key then reduces (aggregates) each group
+data Group = Group {
+    gColl :: Collection,
+    gKey :: GroupKey,  -- ^ Fields to group by
+    gReduce :: Javascript,  -- ^ @(doc, agg) -> ()@. The reduce function reduces (aggregates) the objects iterated. Typical operations of a reduce function include summing and counting. It takes two arguments, the current document being iterated over and the aggregation value, and updates the aggregate value.
+    gInitial :: Document,  -- ^ @agg@. Initial aggregation value supplied to reduce
+    gCond :: Selector,  -- ^ Condition that must be true for a row to be considered. @[]@ means always true.
+    gFinalize :: Maybe Javascript  -- ^ @agg -> () | result@. 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).
+    } deriving (Show, Eq)
+
+data GroupKey = Key [Label] | KeyF Javascript  deriving (Show, Eq)
+-- ^ Fields to group by, or function (@doc -> key@) returning a "key object" to be used as the grouping key. Use 'KeyF' 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
+groupDocument Group{..} =
+    ("finalize" =? gFinalize) ++ [
+    "ns" =: gColl,
+    case gKey of Key k -> "key" =: map (=: True) k; KeyF f -> "$keyf" =: f,
+    "$reduce" =: gReduce,
+    "initial" =: gInitial,
+    "cond" =: gCond ]
+
+group :: (MonadIO m) => Group -> Action m [Document]
+-- ^ Execute group query and return resulting aggregate value for each distinct key
+group g = at "retval" <$> runCommand ["group" =: groupDocument g]
+
+-- ** MapReduce
+
+-- | Maps every document in collection to a list of (key, value) pairs, then for each unique key reduces all its associated values to a single result. There are additional parameters that may be set to tweak this basic operation.
+-- This implements the latest version of map-reduce that requires MongoDB 1.7.4 or greater. To map-reduce against an older server use 'runCommand' directly as described in http://www.mongodb.org/display/DOCS/MapReduce.
+data MapReduce = MapReduce {
+    rColl :: Collection,
+    rMap :: MapFun,
+    rReduce :: ReduceFun,
+    rSelect :: Selector,  -- ^ Operate on only those documents selected. Default is @[]@ meaning all documents.
+    rSort :: Order,  -- ^ Default is @[]@ meaning no sort
+    rLimit :: Limit,  -- ^ Default is 0 meaning no limit
+    rOut :: MROut,  -- ^ Output to a collection with a certain merge policy. Default is no collection ('Inline'). Note, you don't want this default if your result set is large.
+    rFinalize :: Maybe FinalizeFun,  -- ^ 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)
+
+type MapFun = Javascript
+-- ^ @() -> void@. The map function references the variable @this@ to inspect the current object under consideration. The function must call @emit(key,value)@ at least once, but may be invoked any number of times, as may be appropriate.
+
+type ReduceFun = Javascript
+-- ^ @(key, [value]) -> value@. The reduce function receives a key and an array of values and returns an aggregate result value. The MapReduce engine may invoke reduce functions iteratively; thus, these functions must be idempotent.  That is, the following must hold for your reduce function: @reduce(k, [reduce(k,vs)]) == reduce(k,vs)@. 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.
+
+type FinalizeFun = 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.
+
+data MROut =
+      Inline -- ^ Return results directly instead of writing them to an output collection. Results must fit within 16MB limit of a single document
+    | Output MRMerge Collection (Maybe Database) -- ^ Write results to given collection, in other database if specified. Follow merge policy when entry already exists
+    deriving (Show, Eq)
+
+data MRMerge =
+      Replace  -- ^ Clear all old data and replace it with new data
+    | Merge  -- ^ Leave old data but overwrite entries with the same key with new data
+    | Reduce  -- ^ Leave old data but combine entries with the same key via MR's reduce function
+    deriving (Show, Eq)
+
+type MRResult = Document
+-- ^ Result of running a MapReduce has some stats besides the output. See http://www.mongodb.org/display/DOCS/MapReduce#MapReduce-Resultobject
+
+mrDocument :: MapReduce -> Document
+-- ^ Translate MapReduce data into expected document form
+mrDocument MapReduce{..} =
+    ("mapreduce" =: rColl) :
+    ("out" =: mrOutDoc rOut) :
+    ("finalize" =? rFinalize) ++ [
+    "map" =: rMap,
+    "reduce" =: rReduce,
+    "query" =: rSelect,
+    "sort" =: rSort,
+    "limit" =: (fromIntegral rLimit :: Int),
+    "scope" =: rScope,
+    "verbose" =: rVerbose ]
+
+mrOutDoc :: MROut -> Document
+-- ^ Translate MROut into expected document form
+mrOutDoc Inline = ["inline" =: (1 :: Int)]
+mrOutDoc (Output mrMerge coll mDB) = (mergeName mrMerge =: coll) : mdb mDB where
+    mergeName Replace = "replace"
+    mergeName Merge = "merge"
+    mergeName Reduce = "reduce"
+    mdb Nothing = []
+    mdb (Just db) = ["db" =: db]
+
+mapReduce :: Collection -> MapFun -> ReduceFun -> MapReduce
+-- ^ MapReduce on collection with given map and reduce functions. Remaining attributes are set to their defaults, which are stated in their comments.
+mapReduce col map' red = MapReduce col map' red [] [] 0 Inline Nothing [] False
+
+runMR :: MonadIO m => MapReduce -> Action m Cursor
+-- ^ Run MapReduce and return cursor of results. Error if map/reduce fails (because of bad Javascript)
+runMR mr = do
+    res <- runMR' mr
+    case look "result" res of
+        Just (String coll) -> find $ query [] coll
+        Just (Doc doc) -> useDb (at "db" doc) $ find $ query [] (at "collection" doc)
+        Just x -> error $ "unexpected map-reduce result field: " ++ show x
+        Nothing -> newCursor "" "" 0 $ return $ Batch (Just 0) 0 (at "results" res)
+
+runMR' :: (MonadIO m) => MapReduce -> Action m MRResult
+-- ^ Run MapReduce and return a MR result document containing stats and the results if Inlined. Error if the map/reduce failed (because of bad Javascript).
+runMR' mr = do
+    doc <- runCommand (mrDocument mr)
+    return $ if true1 "ok" doc then doc else error $ "mapReduce error:\n" ++ show doc ++ "\nin:\n" ++ show mr
+
+-- * Command
+
+type Command = Document
+-- ^ A command is a special query or action against the database. See <http://www.mongodb.org/display/DOCS/Commands> for details.
+
+runCommand :: (MonadIO m) => Command -> Action m Document
+runCommand params = do
+    pipe <- asks mongoPipe
+    if isHandshake params || maxWireVersion (P.serverData pipe) < 17
+      then runCommandLegacy pipe params
+      else runCommand' pipe params
+
+runCommandLegacy :: MonadIO m => Pipe -> Selector -> ReaderT MongoContext m Document
+runCommandLegacy pipe params = do
+    qr <- queryRequest False (query params "$cmd") {limit = 1}
+    rq <- liftIO $ request pipe [] qr
+    Batch _ _ docs <- liftDB $ fulfill rq
+    case docs of
+      [doc] -> pure doc
+      _ -> error $ "Nothing returned for command: " <> show params
+
+runCommand' :: MonadIO m => Pipe -> Selector -> ReaderT MongoContext m Document
+runCommand' pipe params = do
+    ctx <- ask
+    rq <- liftIO $ requestOpMsg pipe ( Req (P.Message (mongoDatabase ctx) params), Just 1) []
+    Batch _ _ docs <- liftDB $ fulfill rq
+    case docs of
+      [doc] -> pure doc
+      _ -> error $ "Nothing returned for command: " <> show params
+
+runCommand1 :: (MonadIO m) => Text -> Action m Document
+-- ^ @runCommand1 foo = runCommand [foo =: 1]@
+runCommand1 c = runCommand [c =: (1 :: Int)]
+
+eval :: (MonadIO m, Val v) => Javascript -> Action m v
+-- ^ Run code on server
+eval code = do
+    p <- asks mongoPipe
+    let sd = P.serverData p
+    if maxWireVersion sd <= 7
+      then at "retval" <$> runCommand ["$eval" =: code]
+      else error "The command db.eval() has been removed since MongoDB 4.2"
+
+modifyMVar :: MVar a -> (a -> Action IO (a, b)) -> Action IO b
+modifyMVar v f = do
+  ctx <- ask
+  liftIO $ MV.modifyMVar v (\x -> runReaderT (f x) ctx)
+
+mkWeakMVar :: MVar a -> Action IO () -> Action IO (Weak (MVar a))
+mkWeakMVar m closing = do
+  ctx <- ask
+
 #if MIN_VERSION_base(4,6,0)
   liftIO $ MV.mkWeakMVar m $ runReaderT closing ctx
 #else
diff --git a/Database/MongoDB/Transport/Tls.hs b/Database/MongoDB/Transport/Tls.hs
--- a/Database/MongoDB/Transport/Tls.hs
+++ b/Database/MongoDB/Transport/Tls.hs
@@ -1,6 +1,5 @@
 {-# LANGUAGE CPP #-}
 {-# LANGUAGE OverloadedStrings #-}
-{-# LANGUAGE RecordWildCards #-}
 
 #if (__GLASGOW_HASKELL__ >= 706)
 {-# LANGUAGE RecursiveDo #-}
@@ -21,40 +20,44 @@
 barely tested. The current implementation doesn't verify server's identity.
 It only allows you to connect to a mongodb server using TLS protocol.
 -}
+
 module Database.MongoDB.Transport.Tls
-(connect)
+( connect
+, connectWithTlsParams
+)
 where
 
 import Data.IORef
-import Data.Monoid
 import qualified Data.ByteString as ByteString
 import qualified Data.ByteString.Lazy as Lazy.ByteString
 import Data.Default.Class (def)
-import Control.Applicative ((<$>))
 import Control.Exception (bracketOnError)
 import Control.Monad (when, unless)
 import System.IO
-import Database.MongoDB (Pipe)
-import Database.MongoDB.Internal.Protocol (newPipeWith)
+import Database.MongoDB.Internal.Protocol (Pipe, newPipeWith)
 import Database.MongoDB.Transport (Transport(Transport))
 import qualified Database.MongoDB.Transport as T
 import System.IO.Error (mkIOError, eofErrorType)
-import Network (connectTo, HostName, PortID)
+import Database.MongoDB.Internal.Network (connectTo, HostName, PortID)
 import qualified Network.TLS as TLS
 import qualified Network.TLS.Extra.Cipher as TLS
 import Database.MongoDB.Query (access, slaveOk, retrieveServerData)
 
 -- | Connect to mongodb using TLS
 connect :: HostName -> PortID -> IO Pipe
-connect host port = bracketOnError (connectTo host port) hClose $ \handle -> do
-
-  let params = (TLS.defaultParamsClient host "")
+connect host port = connectWithTlsParams params host port
+  where
+    params = (TLS.defaultParamsClient host "")
         { TLS.clientSupported = def
-            { TLS.supportedCiphers = TLS.ciphersuite_all}
+            { TLS.supportedCiphers = TLS.ciphersuite_default }
         , TLS.clientHooks = def
-            { TLS.onServerCertificate = \_ _ _ _ -> return []}
+            { TLS.onServerCertificate = \_ _ _ _ -> return [] }
         }
-  context <- TLS.contextNew handle params
+
+-- | Connect to mongodb using TLS using provided TLS client parameters
+connectWithTlsParams :: TLS.ClientParams -> HostName -> PortID -> IO Pipe
+connectWithTlsParams clientParams host port = bracketOnError (connectTo host port) hClose $ \handle -> do
+  context <- TLS.contextNew handle clientParams
   TLS.handshake context
 
   conn <- tlsConnection context
diff --git a/mongoDB.cabal b/mongoDB.cabal
--- a/mongoDB.cabal
+++ b/mongoDB.cabal
@@ -1,5 +1,5 @@
 Name:           mongoDB
-Version:        2.3.0.5
+Version:        2.7.1.4
 Synopsis:       Driver (client) for MongoDB, a free, scalable, fast, document
                 DBMS
 Description:    This package lets you connect to MongoDB servers and
@@ -19,14 +19,19 @@
 Stability:      alpha
 Extra-Source-Files: CHANGELOG.md
 
+-- Imitated from https://github.com/mongodb-haskell/bson/pull/18
+Flag _old-network
+  description: Control whether to use <http://hackage.haskell.org/package/network-bsd network-bsd>
+  manual: False
+
 Library
   GHC-options:      -Wall
   default-language: Haskell2010
 
   Build-depends:      array -any
-                    , base <5
+                    , base >=4.13 && <5
                     , binary -any
-                    , bson >= 0.3 && < 0.4
+                    , bson >= 0.3 && < 0.5
                     , text
                     , bytestring -any
                     , containers -any
@@ -34,7 +39,6 @@
                     , conduit-extra
                     , mtl >= 2
                     , cryptohash -any
-                    , network -any
                     , parsec -any
                     , random -any
                     , random-shuffle -any
@@ -42,8 +46,9 @@
                     , monad-control >= 0.3.1
                     , lifted-base >= 0.1.0.3
                     , pureMD5
+                    , stm
                     , tagged
-                    , tls >= 1.2.0
+                    , tls >= 1.3.0
                     , time
                     , data-default-class -any
                     , transformers
@@ -51,8 +56,19 @@
                     , hashtables >= 1.1.2.0
                     , base16-bytestring >= 0.1.1.6
                     , base64-bytestring >= 1.0.0.1
-                    , nonce >= 1.0.2
+                    , nonce >= 1.0.5
+                    , fail
+                    , dns
+                    , http-types
 
+  if flag(_old-network)
+     -- "Network.BSD" is only available in network < 2.9
+     build-depends: network < 2.9
+  else
+     -- "Network.BSD" has been moved into its own package `network-bsd`
+     build-depends: network >= 3.0
+                    , network-bsd >= 2.7 && < 2.9
+
   Exposed-modules:  Database.MongoDB
                     Database.MongoDB.Admin
                     Database.MongoDB.Connection
@@ -60,7 +76,8 @@
                     Database.MongoDB.Query
                     Database.MongoDB.Transport
                     Database.MongoDB.Transport.Tls
-  Other-modules:    Database.MongoDB.Internal.Protocol
+  Other-modules:    Database.MongoDB.Internal.Network
+                    Database.MongoDB.Internal.Protocol
                     Database.MongoDB.Internal.Util
 
 Source-repository head
@@ -97,22 +114,38 @@
                     , base64-bytestring
                     , base16-bytestring
                     , binary -any
-                    , bson >= 0.3 && < 0.4
+                    , bson >= 0.3 && < 0.5
+                    , conduit
+                    , conduit-extra
                     , data-default-class -any
                     , text
                     , bytestring -any
                     , containers -any
                     , mtl >= 2
                     , cryptohash -any
-                    , network -any
-                    , nonce
+                    , nonce >= 1.0.5
+                    , stm
                     , parsec -any
                     , random -any
                     , random-shuffle -any
                     , monad-control >= 0.3.1
                     , lifted-base >= 0.1.0.3
+                    , transformers
                     , transformers-base >= 0.4.1
                     , hashtables >= 1.1.2.0
+                    , fail
+                    , dns
+                    , http-types
                     , criterion
+                    , tls >= 1.3.0
+
+  if flag(_old-network)
+     -- "Network.BSD" is only available in network < 2.9
+     build-depends: network < 2.9
+  else
+     -- "Network.BSD" has been moved into its own package `network-bsd`
+     build-depends: network >= 3.0
+                    , network-bsd >= 2.7 && < 2.9
+
   default-language: Haskell2010
   default-extensions: OverloadedStrings
diff --git a/test/Main.hs b/test/Main.hs
--- a/test/Main.hs
+++ b/test/Main.hs
@@ -1,19 +1,17 @@
 module Main where
 
-import Database.MongoDB.Admin (serverVersion)
-import Database.MongoDB.Connection (connect, host)
-import Database.MongoDB.Query (access, slaveOk)
-import Data.Text (unpack)
+import Control.Exception (assert)
+import Control.Monad (when)
+import Data.Maybe (isJust)
+import qualified Spec
+import System.Environment (getEnv, lookupEnv)
 import Test.Hspec.Runner
-import System.Environment (getEnv)
-import System.IO.Error (catchIOError)
 import TestImport
-import qualified Spec
 
 main :: IO ()
 main = do
-  mongodbHost <- getEnv mongodbHostEnvVariable `catchIOError` (\_ -> return "localhost")
-  p <- connect $ host mongodbHost
-  version <- access p slaveOk "admin" serverVersion
-  putStrLn $ "Running tests with mongodb version: " ++ (unpack version)
+  version <- getEnv "MONGO_VERSION"
+  when (version == "mongo_atlas") $ do
+    connection_string <- lookupEnv "CONNECTION_STRING"
+    pure $ assert (isJust connection_string) ()
   hspecWith defaultConfig Spec.spec
diff --git a/test/QuerySpec.hs b/test/QuerySpec.hs
--- a/test/QuerySpec.hs
+++ b/test/QuerySpec.hs
@@ -4,9 +4,10 @@
 module QuerySpec (spec) where
 import Data.String (IsString(..))
 import TestImport
+import Control.Concurrent (threadDelay)
 import Control.Exception
 import Control.Monad (forM_, when)
-import System.Environment (getEnv)
+import System.Environment (getEnv, lookupEnv)
 import System.IO.Error (catchIOError)
 import qualified Data.List as L
 
@@ -16,12 +17,26 @@
 testDBName = "mongodb-haskell-test"
 
 db :: Action IO a -> IO a
-db action = do
+db action = bracket start end inbetween
+ where
+  start = lookupEnv "CONNECTION_STRING" >>= getPipe
+  end (_, pipe) = close pipe
+  inbetween (testuser, pipe) = do
+    logged_in <-
+      access pipe master "admin" $ do
+        auth (u_name testuser) (u_passwd testuser)
+    assert logged_in $ pure ()
+    access pipe master testDBName action
+  getPipe Nothing = do
+    let user = TestUser "testadmin" "123"
     mongodbHost <- getEnv mongodbHostEnvVariable `catchIOError` (\_ -> return "localhost")
     pipe <- connect (host mongodbHost)
-    result <- access pipe master testDBName action
-    close pipe
-    return result
+    pure (user, pipe)
+  getPipe (Just cs) = do
+    let creds = extractMongoAtlasCredentials . T.pack $ cs
+        user = TestUser "testadmin" (atlas_password creds)
+    pipe <- connectAtlas creds
+    pure (user, pipe)
 
 getWireVersion :: IO Int
 getWireVersion = db $ do
@@ -43,6 +58,21 @@
                               ]
   return ()
 
+insertUsers :: IO ()
+insertUsers = db $
+  insertAll_ "users" [ ["_id" =: "jane", "joined" =: parseDate "2011-03-02", "likes" =: ["golf", "racquetball"]]
+                     , ["_id" =: "joe", "joined" =: parseDate "2012-07-02", "likes" =: ["tennis", "golf", "swimming"]]
+                     , ["_id" =: "jill", "joined" =: parseDate "2013-11-17", "likes" =: ["cricket", "golf"]]
+                     ]
+
+pendingIfMongoVersion :: ((Integer, Integer) -> Bool) -> SpecWith () -> Spec
+pendingIfMongoVersion invalidVersion = before $ do
+  version <- db $ extractVersion . T.splitOn "." . at "version" <$> runCommand1 "buildinfo"
+  when (invalidVersion version) $ pendingWith "This test does not run in the current database version"
+  where
+    extractVersion (major:minor:_) = (read $ T.unpack major, read $ T.unpack minor)
+    extractVersion _               = error "Invalid version specification"
+
 bigDocument :: Document
 bigDocument = (flip map) [1..10000] $ \i -> (fromString $ "team" ++ (show i)) =: ("team " ++ (show i) ++ " name")
 
@@ -52,6 +82,8 @@
 hugeDocument :: Document
 hugeDocument = (flip map) [1..1000000] $ \i -> (fromString $ "team" ++ (show i)) =: ("team " ++ (show i) ++ " name")
 
+data TestUser = TestUser {u_name :: T.Text, u_passwd :: T.Text}
+
 spec :: Spec
 spec = around withCleanDatabase $ do
   describe "useDb" $ do
@@ -60,6 +92,16 @@
       db thisDatabase `shouldReturn` testDBName
       db (useDb anotherDBName thisDatabase) `shouldReturn` anotherDBName
 
+  describe "collectionWithDot" $ do
+    it "uses a collection with dots in the name" $ do
+      -- Dots in collection names are disallowed from Mongo 6 on
+      mongo_version <- getEnv "MONGO_VERSION"
+      when (mongo_version `elem` ["mongo:5.0", "mongo:4.0"]) $ do
+        let collec = "collection.with.dot"
+        _id <- db $ insert collec ["name" =: "jack", "color" =: "blue"]
+        Just doc <- db $ findOne (select ["name" =: "jack"] collec)
+        doc !? "color" `shouldBe` Just "blue"
+
   describe "insert" $ do
     it "inserts a document to the collection and returns its _id" $ do
       _id <- db $ insert "team" ["name" =: "Yankees", "league" =: "American"]
@@ -72,6 +114,21 @@
       db (count $ select ["name" =: "Yankees", "league" =: "American"] "team") `shouldReturn` 1
       _id `shouldBe` ()
 
+  describe "upsert" $ do
+    it "upserts a document twice with the same spec" $ do
+      let q = select ["name" =: "jack"] "users"
+      db $ upsert q ["color" =: "blue", "name" =: "jack"]
+      -- since there is no way to ask for a ack, we must wait for "a sufficient time"
+      -- for the write to be visible
+      threadDelay 10000
+      db (rest =<< find (select [] "users")) >>= print
+      db (count $ select ["name" =: "jack"] "users") `shouldReturn` 1
+      db $ upsert q ["color" =: "red", "name" =: "jack"]
+      threadDelay 10000
+      db (count $ select ["name" =: "jack"] "users") `shouldReturn` 1
+      Just doc <- db $ findOne (select ["name" =: "jack"] "users")
+      doc !? "color" `shouldBe` Just "red"
+
   describe "insertMany" $ do
     it "inserts documents to the collection and returns their _ids" $ do
       (_id1:_id2:_) <- db $ insertMany "team" [ ["name" =: "Yankees", "league" =: "American"]
@@ -428,13 +485,33 @@
       collections <- db $ allCollections
       liftIO $ (L.sort collections) `shouldContain` ["team1", "team2", "team3"]
 
-  describe "aggregate" $ do
+  describe "aggregate" $ before_ insertUsers $
     it "aggregates to normalize and sort documents" $ do
-      db $ insertAll_ "users" [ ["_id" =: "jane", "joined" =: parseDate "2011-03-02", "likes" =: ["golf", "racquetball"]]
-                              , ["_id" =: "joe", "joined" =: parseDate "2012-07-02", "likes" =: ["tennis", "golf", "swimming"]]
-                              , ["_id" =: "jill", "joined" =: parseDate "2013-11-17", "likes" =: ["cricket", "golf"]]
-                              ]
       result <- db $ aggregate "users" [ ["$project" =: ["name" =: ["$toUpper" =: "$_id"], "_id" =: 0]]
                                        , ["$sort" =: ["name" =: 1]]
                                        ]
       result `shouldBe` [["name" =: "JANE"], ["name" =: "JILL"], ["name" =: "JOE"]]
+
+  -- This feature was introduced in MongoDB version 3.2
+  -- https://docs.mongodb.com/manual/reference/command/find/
+  describe "findCommand" $ pendingIfMongoVersion (< (3,2)) $
+    context "when mongo version is 3.2 or superior" $ before insertUsers $ do
+      it "fetches all the records" $ do
+          result <- db $ rest =<< findCommand (select [] "users")
+          length result `shouldBe` 3
+
+      it "filters the records" $ do
+        result <- db $ rest =<< findCommand (select ["_id" =: "joe"] "users")
+        length result `shouldBe` 1
+
+      it "projects the records" $ do
+        result <- db $ rest =<< findCommand
+          (select [] "users") { project = [ "_id" =: 1 ] }
+        result `shouldBe` [["_id" =: "jane"], ["_id" =: "joe"], ["_id" =: "jill"]]
+
+      it "sorts the records" $ do
+        result <- db $ rest =<< findCommand
+          (select [] "users") { project = [ "_id" =: 1 ]
+                              , sort    = [ "_id" =: 1 ]
+                              }
+        result `shouldBe` [["_id" =: "jane"], ["_id" =: "jill"], ["_id" =: "joe"]]
diff --git a/test/TestImport.hs b/test/TestImport.hs
--- a/test/TestImport.hs
+++ b/test/TestImport.hs
@@ -1,15 +1,18 @@
 {-# LANGUAGE CPP #-}
+{-# LANGUAGE LambdaCase #-}
 
 module TestImport (
   module TestImport,
-  module Export
+  module Export,
 ) where
 
-import Test.Hspec as Export hiding (Selector)
-import Database.MongoDB as Export
-import Control.Monad.Trans as Export (MonadIO, liftIO) 
+import Control.Exception (SomeException (SomeException), try)
+import Control.Monad.Trans as Export (MonadIO, liftIO)
+import qualified Data.Text as T
 import Data.Time (ParseTime, UTCTime)
 import qualified Data.Time as Time
+import Database.MongoDB as Export
+import Test.Hspec as Export hiding (Selector)
 
 -- We support the old version of time because it's easier than trying to use
 -- only the new version and test older GHC versions.
@@ -20,7 +23,7 @@
 import Data.Maybe (fromJust)
 #endif
 
-parseTime :: ParseTime t => String -> String -> t
+parseTime :: (ParseTime t) => String -> String -> t
 #if MIN_VERSION_time(1,5,0)
 parseTime = Time.parseTimeOrError True defaultTimeLocale
 #else
@@ -35,3 +38,35 @@
 
 mongodbHostEnvVariable :: String
 mongodbHostEnvVariable = "HASKELL_MONGODB_TEST_HOST"
+
+data MongoAtlas = MongoAtlas
+  { atlas_host :: T.Text
+  , atlas_user :: T.Text
+  , atlas_password :: T.Text
+  }
+
+extractMongoAtlasCredentials :: T.Text -> MongoAtlas
+extractMongoAtlasCredentials cs =
+  let s = T.drop 14 cs
+      [u, s'] = T.splitOn ":" s
+      [p, h] = T.splitOn "@" s'
+   in MongoAtlas h u p
+
+connectAtlas :: MongoAtlas -> IO Pipe
+connectAtlas (MongoAtlas h _ _) = do
+  repset <- openReplicaSetSRV' $ T.unpack h
+  primaryOrSecondary repset >>= \case
+    Just pipe -> pure pipe
+    Nothing -> ioError $ error "Unable to acquire pipe from MongoDB Atlas' replicaset"
+ where
+  primaryOrSecondary rep =
+    try (primary rep) >>= \case
+      Left (SomeException err) -> do
+        print $
+          "Failed to acquire primary replica, reason:"
+            ++ show err
+            ++ ". Moving to second..."
+        try (secondaryOk rep) >>= \case
+          Left (SomeException _) -> pure Nothing
+          Right pipe -> pure $ Just pipe
+      Right pipe -> pure $ Just pipe
