C# 使用BsonDocument BsonArray将包含json文档数组的json文件插入ammongodb集合

C# 使用BsonDocument BsonArray将包含json文档数组的json文件插入ammongodb集合,c#,json,mongodb,.net-core,bsondocument,C#,Json,Mongodb,.net Core,Bsondocument,我正在研究一种异步读取json文件(包含json对象数组)内容并将其插入mongodb集合的方法,但我无法找出问题所在。调试时没有错误,但我的集合仍然为空 public async void InsertDocumentsInCollection(string File) { string text = System.IO.File.ReadAllText(File);

我正在研究一种异步读取json文件(包含json对象数组)内容并将其插入mongodb集合的方法,但我无法找出问题所在。调试时没有错误,但我的集合仍然为空

 public async void InsertDocumentsInCollection(string File)
                    {
                        string text = System.IO.File.ReadAllText(File);
                        IEnumerable<BsonDocument> doc = BsonSerializer.Deserialize<BsonArray>(text).Select(p => p.AsBsonDocument);
            //Name of the collection is Cars
                        var collection = _database.GetCollection<BsonDocument>("Cars");
                        await collection.InsertManyAsync(doc);  
                    }
public异步void InsertDocumentsInCollection(字符串文件)
{
string text=System.IO.File.ReadAllText(文件);
IEnumerable doc=BsonSerializer.Deserialize(text).Select(p=>p.AsBsonDocument);
//收藏的名字是汽车
var collection=_database.GetCollection(“Cars”);
等待收集。插入manyasync(doc);
}

我试图用以下内容重现该问题,但效果很好。可能是文本文件的内容有问题。你能发一份文件的样本吗

使用MongoDB.Bson;
使用MongoDB.Bson.Serialization;
使用MongoDB.Driver;
使用System.IO;
使用System.Linq;
使用System.Threading.Tasks;
命名空间堆栈溢出
{
公共课程
{
专用静态异步任务主(字符串[]args)
{
var content=File.ReadAllText(“E:\\Downloads\\cars.txt”)//https://mongoplayground.net/p/LY0W7vjDuvp
var docs=BsonSerializer.Deserialize(content.Select)(p=>p.AsBsonDocument);
var集合=新的MongoClient(“mongodb://localhost")
.GetDatabase(“测试”)
.GetCollection(“汽车”);
等待收集。InsertManyAsync(文档);
}
}
}

我试图用以下内容重现该问题,但效果很好。可能是文本文件的内容有问题。你能发一份文件的样本吗

使用MongoDB.Bson;
使用MongoDB.Bson.Serialization;
使用MongoDB.Driver;
使用System.IO;
使用System.Linq;
使用System.Threading.Tasks;
命名空间堆栈溢出
{
公共课程
{
专用静态异步任务主(字符串[]args)
{
var content=File.ReadAllText(“E:\\Downloads\\cars.txt”)//https://mongoplayground.net/p/LY0W7vjDuvp
var docs=BsonSerializer.Deserialize(content.Select)(p=>p.AsBsonDocument);
var集合=新的MongoClient(“mongodb://localhost")
.GetDatabase(“测试”)
.GetCollection(“汽车”);
等待收集。InsertManyAsync(文档);
}
}
}
@TropicalViking仍然有效:除了重复的Id之外,上述代码也有效。是否确实使用mongoclient连接到正确的服务器/db/集合组合?此外,如果在反序列化和检查后设置断点,
doc
变量是否包含反序列化的bsondocuments?@TropicalViking仍然有效:除了重复的Id之外,上面的代码还在工作。是否确实使用mongoclient连接到正确的服务器/db/集合组合?另外,如果在反序列化和检查后设置断点,
doc
变量是否包含反序列化的bsondocuments?