MongoDB GetCollection 方法是否将整个集合加载到 RAM 或引用中? C#
Does MongoDB GetCollection method load the entire collection into RAM or a reference? C#
我有一个存储库 class 处理 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");
}
然后我会做类似的事情:
_collection.Insert(locationObject)
在class的其他方法中。
考虑到内存有限,我想知道这是否可取?如果没有,是否有一种明智的方法可以直接保存到数据库而无需加载集合。
GetCollection
不会加载集合,甚至 Find()
也不会。事实上,你必须在 anything 实际从数据库加载之前开始迭代 MongoCursor
,即便如此,它也不会加载整个集合,而只会加载批次可配置大小。
如果您想实际加载整个集合,您可以在 MongoCursor
上调用 ToList()
,例如,当然这很少有意义。
我有一个存储库 class 处理 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");
}
然后我会做类似的事情:
_collection.Insert(locationObject)
在class的其他方法中。
考虑到内存有限,我想知道这是否可取?如果没有,是否有一种明智的方法可以直接保存到数据库而无需加载集合。
GetCollection
不会加载集合,甚至 Find()
也不会。事实上,你必须在 anything 实际从数据库加载之前开始迭代 MongoCursor
,即便如此,它也不会加载整个集合,而只会加载批次可配置大小。
如果您想实际加载整个集合,您可以在 MongoCursor
上调用 ToList()
,例如,当然这很少有意义。