2017-03-08 40 views
1

我剛開始使用MongoDB中與大量數據的處理我的新project.I剛建立的數據庫和已安裝的C#驅動程序的MongoDB和這裏的結果是什麼,我試圖MongoDB中未能插入我的所有記錄

public IHttpActionResult insertSample() 
{ 

    var client = new MongoClient("mongodb://localhost:27017"); 
    var database = client.GetDatabase("reznext"); 
    var collection = database.GetCollection<BsonDocument>("sampledata"); 
    List<BsonDocument> batch = new List<BsonDocument>(); 

    for (int i = 0; i < 300000; i++) 
    { 
     batch.Add(
      new BsonDocument { 
      { "field1", 1 }, 
      { "field2", 2 }, 
      { "field3", 3 }, 
      { "field4", 4 } 
       }); 
    } 
    collection.InsertManyAsync(batch); 
    return Json("OK"); 
} 

但是,當我檢查文檔的集合時,我看到插入的0.3million記錄中只有42k。我使用robomongo作爲客戶端,並且想知道這裏出了什麼問題。每個操作是否存在任何插入限制?

回答

1

你寫異步,不要等待結果。要麼等待它:

collection.InsertManyAsync(batch).Wait(); 

或者使用同步調用:

collection.InsertMany(batch);