Mongodb 无法插入我的所有记录

Mongodb failed to insert all my records

我刚刚开始使用 mongodb,因为我要为我的新 project.I 处理大量数据,刚刚设置了数据库并为 mongodb 安装了 c# 驱动程序,这就是我试过了

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");
}

但是当我检查文档集合时,我只看到 30 万条记录中的 42k inserted.I 使用 robomongo 作为客户端并想知道哪里出了问题 here.Is 每个操作有任何插入限制?

你写异步,不等待结果。要么等等:

collection.InsertManyAsync(batch).Wait();

或使用同步调用:

collection.InsertMany(batch);