C# Driver: Understanding How Data is Retrieved from MongoDB

322 views
Skip to first unread message

Dan

unread,
Apr 10, 2011, 7:06:41 PM4/10/11
to mongodb-user
Been trying to get a handle on MongoDB since shortly. Thanks again for
all the help so far, to everyone.

I'm trying to understand at what point data is retrieved from MongoDB
and how it is retrieved. Let's say I have the following code:

MongoServer server = MongoServer.Create("mongodb://server");
MongoDatabase BBUDatabase = server.GetDatabase("bbu");

MongoCollection<BBU> BBUCollection;
BBUCollection = BBUDatabase.GetCollection<BBU>("bbu");

MongoCursor<BBU> cursor = BBUCollection.FindAll().SetFields("id",
"whichAPI").SetLimit(100);

foreach (BBU ba in cursor)
{
Console.WriteLine(ba.whichAPI);
}


My question: From what I read in the docs, data is NOT retrieved when
the cursor is defined/initialized. Rather, the data is retrieved the
first time there's an actual call to a piece of data in the foreach
loop. Is this correct?

If this is the case, and if that foreach had to loop through 1million
collections, does it have to individually fetch 1 collection at a time
from the database for each iteration? I would think it would try to
grab the entire collection in one fowl swoop (by just doing one big
data fetch, as opposed 1 million). Once it fetched the data, the
foreach loop just iterated through what was fetched and wrote it out.

Obviously I'm missing something. Would appreciate some guidance.

Thanks!



Scott Hernandez

unread,
Apr 10, 2011, 7:09:46 PM4/10/11
to mongod...@googlegroups.com
It retrieves documents in batches using a cursor. The iterator will
check if it needs a new batch, and then it will go get it to continue
on to the next one.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Dan

unread,
Apr 10, 2011, 7:19:49 PM4/10/11
to mongodb-user
Thanks!

How do I know the batch size and how does it determine what the right
size is? Can I set it somewhere?

Scott Hernandez

unread,
Apr 10, 2011, 7:24:21 PM4/10/11
to mongod...@googlegroups.com
You can set the batch size (in # of docs) but by default it send
4/16MB from the server (depending on the version). Unless you have a
good reason to decrease it I wouldn't suggest changing it; it will
send them as big as it can.

Dan

unread,
Apr 10, 2011, 7:37:34 PM4/10/11
to mongodb-user
Thanks for the info!
Reply all
Reply to author
Forward
0 new messages