mongoDB 2.0.2 → 2.0.3
raw patch · 2 files changed
+23/−19 lines, 2 files
Files
- Database/MongoDB/Query.hs +22/−18
- mongoDB.cabal +1/−1
Database/MongoDB/Query.hs view
@@ -21,7 +21,7 @@ -- ** Insert insert, insert_, insertMany, insertMany_, insertAll, insertAll_, -- ** Update- save, replace, repsert, Modifier, modify,+ save, replace, repsert, upsert, Modifier, modify, -- ** Delete delete, deleteOne, -- * Read@@ -305,10 +305,10 @@ -- ** Update save :: (MonadIO m) => Collection -> Document -> Action m ()--- ^ Save document to collection, meaning insert it if its new (has no \"_id\" field) or update it if its not new (has \"_id\" field)+-- ^ 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 -> repsert (Select ["_id" := i] 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@@ -317,7 +317,12 @@ 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> @@ -434,7 +439,7 @@ return $ case eres of Left l -> Left l Right r -> case r of- -- mongoDB manual says this is only possible when update is True+ -- only possible when upsert is True and new is False Nothing -> Left "findAndModify: impossible null result" Just doc -> Right doc @@ -462,21 +467,20 @@ , "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- Just doc@(_:_) -> case lookupErr result of- Just e -> leftErr e- Nothing -> Right (Just doc)- _ -> case famOpts of- FamUpdate { famUpsert = True, famNew = True } -> Right Nothing- _ -> leftErr $ show result+ 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: no document found: "- `mappend` show collection- `mappend` "from query: " `mappend` show sel- `mappend` err+ 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
mongoDB.cabal view
@@ -1,5 +1,5 @@ Name: mongoDB-Version: 2.0.2+Version: 2.0.3 Synopsis: Driver (client) for MongoDB, a free, scalable, fast, document DBMS Description: This package lets you connect to MongoDB servers and