MongoDB GetCollection method loads entire collection into RAM or reference? FROM#

I have a repository class that handles all database functionality for MongoDB, this is the constructor implementation:

public LocationRepository(string connectionString)
{
    if (string.IsNullOrWhiteSpace(connectionString))
    {
        connectionString = "mongodb://localhost:27017";
    }

    _client = new MongoClient(connectionString);
    _server = _client.GetServer();
    _database = _server.GetDatabase("locDb");
    _collection = _database.GetCollection<Location>("Location");
}

      

Then I do things like:

_collection.Insert(locationObject)

      

in other methods of the class.

I want to know if this is advisable given the limited memory? if not, is there a sensible way to work directly with the DB without having to load the collection.

+2


source to share


1 answer


GetCollection

doesn't load the collection, won't even Find()

. In fact, you will have to start iterating MongoCursor

before anything is loaded from the database, and even then it will not load the entire collection, only batches of custom size.



If you really want to load the entire collection, you can call ToList()

on MongoCursor

, for example, but of course this rarely makes sense.

+3


source







All Articles