简体   繁体   中英

MongoDB C# Driver - How to InsertBatch using a List of Dictionary<string, string>

I'm new to mongodb + C# driver so forgive any naivety on my end.

I'm attempting to do a batch insert on a collection of key-value-pairs and as such my data structure is of type List<Dictionary<string,string>> .

Here's a sample of my persistence code:

public void Persist(string collectionName, List<Dictionary<string, string>> documents)
{
  string connectionString = ConfigurationManager.ConnectionStrings[CONNECTION_STRING_KEY].ConnectionString;
  MongoServer server = MongoServer.Create(connectionString);
  MongoCredentials credentials = new MongoCredentials("MYUser", "MyPassword");
  MongoDatabase myDb = server.GetDatabase("myDb", credentials);

  var collection = myDb .GetCollection(collectionName);

  using (server.RequestStart(myDb ))
  {
    var result = collection.InsertBatch(documents);
  }
}

I get an error about serialization:

MongoDB.Bson.BsonSerializationException: Serializer DictionarySerializer expected serialization options of type DictionarySerializationOptions, not DocumentSerializationOptions.

Am I missing settings?

EDIT: More Information

My dictionaries are my entities. Meaning, instead of created an object to hold properties, I just dump them into a Dictionary . From mongo documentation, it appears as this should just translate to a mongo Document .

FURTHER EDIT: Twist Question

I was able to get a single instance to insert by changing the using statement to:

using (server.RequestStart(myDb))
  {
    foreach(var doc in documents)
      collection.Insert(new BsonDocument(doc));
    //var result = collection.InsertBatch(typeof(Dictionary<string, string>), documents);
  }

However, my concern is performance since under a real scenario I will easily have 10k+ dictionaries. Using this code, is the driver smart enough to batch these? is there a way to keep InsertBatch but accomplish the same thing?

Of course, any help is much appreciated.

Using your new code that uses .Insert , the driver will not batch these inserts and you will get drastically slower performance than an InsertBatch .

Try this instead:

collection.InsertBatch(documents.Select(d => new BsonDocument(d)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM