C# MongoDB InsertBatch作业对象-序列化错误

C# MongoDB InsertBatch作业对象-序列化错误,c#,.net,mongodb,json.net,C#,.net,Mongodb,Json.net,我正在使用一个.NET应用程序并尝试插入到MongoDB。 我正在使用InsertBatch并将Newtonsoft.Json.Linq.JObject的IEnumerable传递给它 我得到的错误是: {"Serializer DictionarySerializer<String, JToken> expected serialization options of type DictionarySerializationOptions, not DocumentSerializa

我正在使用一个.NET应用程序并尝试插入到MongoDB。 我正在使用InsertBatch并将Newtonsoft.Json.Linq.JObject的IEnumerable传递给它

我得到的错误是:

{"Serializer DictionarySerializer<String, JToken> expected serialization options of type DictionarySerializationOptions, not DocumentSerializationOptions."}
{“Serializer DictionarySerializer预期的序列化选项类型为DictionarySerializationOptions,而不是DocumentSerializationOptions。”}
我的代码是:

private void InsertItemsToMongo(IEnumerable<JObject> list)
{
    MongoClient = new MongoClient("mongodb://localhost:27017");
    var myDb = mongo.GetServer().GetDatabase("MyDatabase");
    if (!myDb.CollectionExists("MyStuff");
        myDb.CreateCollection("MyStuff");

    MongoCollection<JObject> myCollection = myDb.GetCollection<JObject>("MyStuff");
    myCollection.InsertBatch(list);
}
private void InsertItemsToMongo(IEnumerable列表)
{
MongoClient=新的MongoClient(“mongodb://localhost:27017");
var myDb=mongo.GetServer().GetDatabase(“MyDatabase”);
如果(!myDb.CollectionExists(“MyStuff”);
myDb.CreateCollection(“MyStuff”);
MongoCollection myCollection=myDb.GetCollection(“MyStuff”);
myCollection.InsertBatch(列表);
}
错误在InsertBatch行抛出

如果您需要任何其他信息,请提供,我只提供了我认为相关的


谢谢!

您不能将JObject插入mongo,您必须将其转换为BsonDocument

var bsonlist = new List<BsonDocument>();
foreach (var obj in list)
{
    bsonlist.Add(BsonDocument.Parse(obj));
}

var myCollection = database.GetCollection("MyStuff");
var doc = BsonArray.Create(bsonlist);
myCollection.InsertBatch(doc);
var bsonlist=newlist();
foreach(列表中的var obj)
{
添加(BsonDocument.Parse(obj));
}
var myCollection=database.GetCollection(“MyStuff”);
var doc=BsonArray.Create(bsonlist);
myCollection.InsertBatch(doc);