无法使用 .Net 驱动程序 2.0 在 mongodb c# 中保存文档

not able to save documents in mongodb c# with .Net driver 2.0

我想将文档保存在一个集合中,我的方法如下,但根本没有保存。

internal static void InitializeDb()
    {
        var db = GetConnection();
        var collection = db.GetCollection<BsonDocument>("locations");
        var locations = new List<BsonDocument>();
        var json = JObject.Parse(File.ReadAllText(@"..\..\test_files\TestData.json"));
        foreach (var d in json["locations"])
        {
            using (var jsonReader = new JsonReader(d.ToString()))
            {
                var context = BsonDeserializationContext.CreateRoot(jsonReader);
                var document = collection.DocumentSerializer.Deserialize(context);
                locations.Add(document);
            }
        }
        collection.InsertManyAsync(locations);
    }

如果我最近做了 async 和 await 然后它 运行,我需要先 运行 然后只测试数据。

为了将来参考,wait() 在异步方法结束时像同步一样工作

internal static void InitializeDb()
{
    var db = GetConnection();
    var collection = db.GetCollection<BsonDocument>("locations");
    var locations = new List<BsonDocument>();
    var json = JObject.Parse(File.ReadAllText(@"..\..\test_files\TestData.json"));
    foreach (var d in json["locations"])
    {
        using (var jsonReader = new JsonReader(d.ToString()))
        {
            var context = BsonDeserializationContext.CreateRoot(jsonReader);
            var document = collection.DocumentSerializer.Deserialize(context);
            locations.Add(document);
        }
    }
    collection.InsertManyAsync(locations).wait();
}