Reputation: 31
I'm trying to iterate over every document in a collection and save them to an excel file. Currently there are 857 documents and I have confirmed this in Compass, but only 756 are being returned. What would stop all the documents from being returned?
At first I thought it was to do with my object mapping, but I reverted back to Bsondocuments and I get the same result. Is there something I am missing here?
var db = client.GetDatabase("database");
var collection = db.GetCollection<BsonDocument>("collection");
var filter = new BsonDocument();
using (var cursor = collection.Find(filter).ToCursor())
while (cursor.MoveNext())
{
int i = 1;
foreach (var doc in cursor.Current)
{
;
sheet.Cells["A" + i.ToString()].Value = doc.ToString();
i++;
Console.WriteLine("Documents found: " + i);
Documents found: 757
Upvotes: 0
Views: 755
Reputation: 613
Mongo's cursor returns documents in batches. You move from batch to batch using MoveNext and then processing the documents in Current. The code appears to this, but the counter i is reset to 1 on every batch. You will want to do something like:
var db = client.GetDatabase("database");
var collection = db.GetCollection<BsonDocument>("collection");
var filter = new BsonDocument();
using (var cursor = collection.Find(filter).ToCursor()){
int i = 1;
while (cursor.MoveNext())
{
foreach (var doc in cursor.Current)
{
;
sheet.Cells["A" + i.ToString()].Value = doc.ToString();
i++;
}
}
Console.WriteLine("Documents found: " + i);
}
Alternately it may be easier to do:
int i = 1;
await cursor.ForEachAsync(doc => {
sheet.Cells["A" + i.ToString()].Value = doc.ToString()
i++;
});
Console.WriteLine("Documents found: " + i);
or if you aren't setup for async:
int i = 1;
cursor.ForEachAsync(doc => {
sheet.Cells["A" + i.ToString()].Value = doc.ToString()
i++;
}).Wait();
Console.WriteLine("Documents found: " + i);
Upvotes: 1