Im trying to move data from SQL to MongoDB. Question is related to C# Async Await. What I do is collect 2000 documents first and then insert into MongoDB. I am awaiting my insert call. What I want to do here is instead of waiting I want to continue collecting next 2000 documents but only call the SaveToCollection method only if last call is returned.
int counter = 0, totalCounter = 0;
List<BsonDocument> documentList = new List<BsonDocument>();
while (rdr.Read())
{
Dictionary<string, object> obj = new Dictionary<string, object>();
for (int lp = 0; lp < rdr.FieldCount; lp++)
{
if (rdr.GetValue(lp) != DBNull.Value)
obj.Add(rdr.GetName(lp).ToLower(), rdr.GetValue(lp));
else
obj.Add(rdr.GetName(lp).ToLower(), "");
}
documentList.Add(new BsonDocument(obj));
counter++;
if (counter == 2000)
{
/* I dont want to wait here. Continue to get 2000 more documents but wait if previous call is not completed yet.*/
if (SaveToCollection(documentList, MongoCollectionName).GetAwaiter().GetResult())
{
totalCounter = totalCounter + counter;
}
documentList.Clear();
counter = 0;
}
}