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, Collection, FieldSelector, NumToSkip, NumToReturn, Selector,
QueryOpt(..), QueryOpt(..),
UpdateFlag(..), UpdateFlag(..),
delete, insert, insertMany, query, remove, update, count, countMatching, delete, insert, insertMany, query, remove, update,
-- * Convience collection operations -- * Convience collection operations
find, findOne, quickFind, quickFind', find, findOne, quickFind, quickFind',
-- * Cursor -- * Cursor
@ -51,7 +51,7 @@ import Data.Binary
import Data.Binary.Get import Data.Binary.Get
import Data.Binary.Put import Data.Binary.Put
import Data.Bits 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 as L
import qualified Data.ByteString.Lazy.UTF8 as L8 import qualified Data.ByteString.Lazy.UTF8 as L8
import Data.Int import Data.Int
@ -327,6 +327,19 @@ fromUpdateFlags :: [UpdateFlag] -> Int32
fromUpdateFlags flags = List.foldl (.|.) 0 $ fromUpdateFlags flags = List.foldl (.|.) 0 $
flip fmap flags $ (1 `shiftL`) . fromEnum 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 documents matching /Selector/ from the given /Collection/.
delete :: Connection -> Collection -> Selector -> IO RequestID delete :: Connection -> Collection -> Selector -> IO RequestID
delete c col sel = do delete c col sel = do

2
TODO
View file

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