count, countMatching

This commit is contained in:
Scott R. Parish 2010-01-18 22:13:32 -06:00
parent d7f6cfc348
commit dc99780c03
2 changed files with 15 additions and 4 deletions

View file

@ -37,7 +37,7 @@ module Database.MongoDB
Collection, FieldSelector, NumToSkip, NumToReturn, Selector,
QueryOpt(..),
UpdateFlag(..),
delete, insert, insertMany, query, remove, update,
count, countMatching, delete, insert, insertMany, query, remove, update,
-- * Convience collection operations
find, findOne, quickFind, quickFind',
-- * Cursor
@ -51,7 +51,7 @@ import Data.Binary
import Data.Binary.Get
import Data.Binary.Put
import Data.Bits
import Data.ByteString.Char8 hiding (find)
import Data.ByteString.Char8 hiding (count, find)
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.UTF8 as L8
import Data.Int
@ -327,6 +327,19 @@ fromUpdateFlags :: [UpdateFlag] -> Int32
fromUpdateFlags flags = List.foldl (.|.) 0 $
flip fmap flags $ (1 `shiftL`) . fromEnum
-- | Return the number of documents in /Collection/.
count :: Connection -> Collection -> IO Int64
count c col = countMatching c col BSON.empty
-- | Return the number of documents in /Collection/ matching /Selector/
countMatching :: Connection -> Collection -> Selector -> IO Int64
countMatching c col sel = do
let db = dbFromCol col
col' = colMinusDB col
res <- dbCmd c db $ toBsonDoc [("count", toBson col'),
("query", BsonObject sel)]
return $ fromBson $ fromJust $ BSON.lookup "n" res
-- | Delete documents matching /Selector/ from the given /Collection/.
delete :: Connection -> Collection -> Selector -> IO RequestID
delete c col sel = do

2
TODO
View file

@ -42,8 +42,6 @@ MongoDB
* modify
* replace
* repsert
* getCount()
* getCount(query)
- index operations
* createIndex
* dropIndex