mongoDB 2.0 → 2.0.1
raw patch · 3 files changed
+62/−18 lines, 3 files
Files
- Database/MongoDB.hs +2/−3
- Database/MongoDB/Query.hs +52/−12
- mongoDB.cabal +8/−3
Database/MongoDB.hs view
@@ -3,13 +3,12 @@ Simple example below. Use with language extensions /OvererloadedStrings/ & /ExtendedDefaultRules/. -> {-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-}->+ > import Database.MongoDB > import Control.Monad.Trans (liftIO) > > main = do-> pipe <- runIOE $ connect (host "127.0.0.1")+> pipe <- connect (host "127.0.0.1") > e <- access pipe master "baseball" run > close pipe > print e
Database/MongoDB/Query.hs view
@@ -28,7 +28,9 @@ -- ** Query Query(..), QueryOption(NoCursorTimeout, TailableCursor, AwaitData, Partial), Projector, Limit, Order, BatchSize,- explain, find, findOne, fetch, findAndModify, count, distinct,+ explain, find, findOne, fetch,+ findAndModify, findAndModifyOpts, FindAndModifyOpts(..),+ count, distinct, -- *** Cursor Cursor, nextBatch, next, nextN, rest, closeCursor, isCursorClosed, -- ** Aggregate@@ -404,34 +406,72 @@ -- ^ Same as 'findOne' except throw 'DocNotFound' if none match fetch q = findOne q >>= maybe (liftIO $ throwIO $ DocNotFound $ selection q) return --- | runs the findAndModify command.+data FindAndModifyOpts = FamRemove Bool+ | FamUpdate+ { famUpdate :: Document+ , famNew :: Bool+ , famUpsert :: Bool+ }+ deriving Show++findAndModifyUpdate :: Document -> FindAndModifyOpts+findAndModifyUpdate 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).--- Currently this API does not allow setting the remove option+--+-- see 'findAndModifyOpts' if you want to use findAndModify in a differnt way findAndModify :: MonadIO m => Query -> Document -- ^ updates -> Action m (Either String Document)-findAndModify (Query {+findAndModify q ups = do+ eres <- findAndModifyOpts q (findAndModifyUpdate ups)+ return $ case eres of+ Left l -> Left l+ Right r -> case r of+ -- mongoDB manual says this is only possible when update is True+ 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- }) updates = do+ }) famOpts = do result <- runCommand- [ "findAndModify" := String collection- , "new" := Bool True -- return updated document, not original document+ ([ "findAndModify" := String collection , "query" := Doc sel- , "update" := Doc updates , "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 lookup "value" result of Left err -> leftErr err Right mdoc -> case mdoc of- Nothing -> leftErr $ show result- Just doc -> case lookupErr result of+ Just doc@(_:_) -> case lookupErr result of Just e -> leftErr e- Nothing -> Right doc+ Nothing -> Right (Just doc)+ _ -> case famOpts of+ FamUpdate { famUpsert = True, famNew = True } -> Right Nothing+ _ -> leftErr $ show result where leftErr err = Left $ "findAndModify: no document found: " `mappend` show collection
mongoDB.cabal view
@@ -1,5 +1,5 @@ Name: mongoDB-Version: 2.0+Version: 2.0.1 Synopsis: Driver (client) for MongoDB, a free, scalable, fast, document DBMS Description: This package lets you connect to MongoDB servers and@@ -7,13 +7,14 @@ Database.MongoDB and the tutorial from the homepage. For information about MongoDB itself, see www.mongodb.org. Category: Database-Homepage: http://github.com/selectel/mongodb-haskell+Homepage: https://github.com/mongodb-haskell/mongodb+Bug-reports: https://github.com/mongodb-haskell/mongodb/issues Author: Tony Hannan Maintainer: Fedor Gogolev <knsd@knsd.net> Copyright: Copyright (c) 2010-2012 10gen Inc. License: OtherLicense License-file: LICENSE-Cabal-version: >= 1.2+Cabal-version: >= 1.6 Build-type: Simple Stability: alpha @@ -46,3 +47,7 @@ Database.MongoDB.Internal.Util Database.MongoDB.Query System.IO.Pipeline++Source-repository head+ Type: git+ Location: https://github.com/mongodb-haskell/mongodb