简体   繁体   中英

Does MongoDB GetCollection method load the entire collection into RAM or a reference? C#

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

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");
}

I then do stuff like:

_collection.Insert(locationObject)

in other methods of the class.

I want to know if this is advisable considering limited memory? if not, is there an advisable way to persist directly to the DB without having to load the collection.

GetCollection doesn't load the collection, not even a Find() will. In fact, you'll have to start iterating the MongoCursor before anything is actually loaded from the database, and even then, it won't load the entire collection but only batches of configurable size.

If you wanted to actually load the entire collection, you could call ToList() on the MongoCursor , for instance, but of course that rarely makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM