I'm new to mongodb, and trying to implement Repository pattern using mongodb in my app. So my question is when I query db using below query:
_mongodb.GetCollection<T>(typeof(T).Name.ToLower()).AsQueryable();
Will it fetch all the records into memory or is the result a deferred collection which will get data when I iterate it or use ToList on it?
It is deferred. It won't fetch results into the memory unless you request the data or a perform a manipulation on it. as you mention as example .ToList() or .First() or .Count() or iterate over it.
EDIT:
Even though it might not be documented, you can prove that with a simple test. Enable logging for mongodb and execute the line you mentioned in your question; you shouldn't see a query. and then execute .ToList() in another line and you will see the query in the log.
Enable logging (an example) from the official documentation :
var settings = new MongoClientSettings
{
ClusterConfigurator = cb =>
{
var textWriter = TextWriter.Synchronized(new StreamWriter("mylogfile.txt"));
cb.AddListener(new LogListener(textWriter));
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With