查询数据库会引发异常
本文关键字:异常 数据库 查询 | 更新日期: 2023-09-27 18:14:57
当我向客户端返回大约200000条记录时,服务工作正常。但增加返回的数据集(到100万(会导致服务器端出现以下异常:
System.InvalidOperationException: Server instance localhost:xxxx is no longer connected.
at MongoDB.Driver.MongoServerInstance.AcquireConnection(MongoDatabase database)
in C:'work'10gen'mongodb'mongo-csharp driver'Driver'Core'MongoServerInstance. cs:line 288
查询数据库时发生了一些问题。我有最新的MongoDb驱动程序。ConnectionPoolSize = 900
ConnectionTimeoutSeconds = 3000
以较小的块/页读取数据。有关更多信息,请参阅此问题:
MongoDB-分页
简而言之,你可以这样做:
int currentOffset = 0;
int recordsFetched;
do
{
var results =
collection.Find(query)
.SetSkip(currentOffset)
.SetLimit(100)
.ToList();
currentOffset += 100;
recordsFetched = results.Count;
// Process your data here.
} while(recordsFetched > 0);