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.
By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.
The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.
Is MongoDB Free? You can get started with a MongoDB developer sandbox in MongoDB Atlas for free with basic configuration options. No credit cards are required to provision a cluster, and you can use it to explore and learn more about MongoDB Atlas, the database-as-a-service platform from MongoDB.
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)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With