当我向客户返回约200,000条记录时,该服务正常。但是增加返回的数据(减少到1百万)会导致服务器端出现以下异常:
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
答案 0 :(得分:2)
以较小的块/页读取数据。有关更多信息,请参阅此问题:
简而言之,您可以这样做:
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);