I am a beginner in Haskell and I tend to learn by doing.
Could someone provide me an example of how to use the cursor with nextBatch or nextN?
http://hackage.haskell.org/packages/archive/mongoDB/1.3.1/doc/html/Database-MongoDB-Query.html#g:12
Tried something like this (I tried to write a custom iteration function, where f is a function which I want to map over all documents)
let cursor = Mdb.find (Mdb.select selector collection)
-- consume :: IO ()
consume = do
r <- runAction db $ Mdb.nextBatch =<< cursor
if length (fromRight r) == 0
then return ()
else do
mapM_ f (fromRight r)
consume
consume
Now this runs in an infinite loop, always returning the same set of documents. I guess the cursor is never being modified? The cursor type itself has an MVar as one of it’s elements, first I thought that would be updated when I do a nextBatch, but that never happens.
Maybe I should rewrite the recursion the way it stays in the Action monad (see mongo driver), rather than IO? I am stuck.
When you write
let cursor = Mdb.find (Mdb.select selector collection)it createsAction m Cursorthat will be executed only inAction mcontext, so actually you created new cursor each=<< cursortime.Yes, i think that you should rewrite you function in mostly
Action mcontext. For example: