简体   繁体   English

MongoDB的GetCollection方法是否将整个集合加载到RAM或引用中? C#

[英]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: 我有一个存储库类,可以处理MongoDB的所有数据库功能,这是构造函数的实现:

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. GetCollection不会加载集合,甚至Find()都不会加载。 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. 实际上,在从数据库中实际加载任何内容之前,您必须开始迭代MongoCursor ,即使那样,它也不会加载整个集合,而只会加载可配置大小的批处理。

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. 例如,如果您想实际加载整个集合,则可以在MongoCursor上调用ToList() ,但是当然这几乎没有道理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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