简体   繁体   中英

C# MongoDB Driver OutOfMemoryException

I am trying to read data from a remote MongoDB instance from ac# console application but keep getting an OutOfMemoryException. The collection that I am trying to read data from has about 500,000 records. Does anyone see any issue with the code below:

var mongoCred = MongoCredential.CreateMongoCRCredential("xdb", "x", "x");
var mongoClientSettings = new MongoClientSettings
{
    Credentials = new[] { mongoCred },
    Server = new MongoServerAddress("x-x.mongolab.com", 12345),
};

var mongoClient = new MongoClient(mongoClientSettings);
var mongoDb = mongoClient.GetDatabase("xdb");
var mongoCol = mongoDb.GetCollection<BsonDocument>("Persons");
var list = await mongoCol.Find(new BsonDocument()).ToListAsync();

This is a simple workaround: you can page your results using .Limit(?int) and .Skip(?int); in totNum you have to store the documents number in your collection using

coll.Count(new BsonDocument) /*use the same filter you will apply in the next Find()*/

and then

for (int _i = 0; _i < totNum / 1000 + 1; _i++)
{
    var result = coll.Find(new BsonDocument()).Limit(1000).Skip(_i * 1000).ToList();
    foreach(var item in result)
    {
        /*Write your document in CSV file*/
    }
}

I hope this can help... PS I used 1000 in .Skip() and .Limit() but, obviously, you can use what you want :-)

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