Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import Linq-To-SQL list to MongoDB

I have a very simple LinqToSql list.

var list = DB.Where(c => c.Status.Equals("active")).Select(c => c.Name);

I'm trying to import that list into MongoDB. Here's what I have so far:

const string connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
MongoServer server = client.GetServer();
MongoDatabase database = server.GetDatabase("test");
var collection = database.GetCollection("salesppl");
collection.Insert(list);

I have also tried InsertBatch and Save, but with no luck. Here are the error messages I get for each: InsertBatch

BsonSerializationException: Serializer StringSerializer expected serialization options of type RepresentationSerializationOptions, not DocumentSerializationOptions.

Insert

BsonSerializationException: Serializer EnumerableSerializer expected serialization options of type ArraySerializationOptions, not DocumentSerializationOptions.

Save

InvalidOperationException: Save can only be used with documents that have an Id.

Note: I don't think this is of any relevance, but I'm doing this through LinqPad.

So, how can I save this list to MongoDb?

like image 564
inquisitive_one Avatar asked Nov 01 '22 09:11

inquisitive_one


1 Answers

MongoDB is a document database. Collection holds set of documents, where document is a set of key-value pairs. Simple string is not a valid document (see BSON specification). It's even not valid BSON element, because it does not have a key. It's only a value without key. That's why your attempts to save strings failed. And that's why creating valid document with single element inside (having your string as value) has solved your problem.


Here is detailed explanations on your exceptions. How Mongo inserts documents?

  • it gets document type
  • it searches for serializer of this type
  • it serializes document into binary data
  • it sends binary data to database

So, let's see what happens when you inserting strings. First case is when you trying to insert list of strings instead of documents list:

collection.InsertBatch(list);

Theoretically string also can be considered as document. It has property Length and can be serialized as { Length: 42 } document. But you see exception:

BsonSerializationException: Serializer StringSerializer expected serialization options of type RepresentationSerializationOptions, not DocumentSerializationOptions.

String is one of BSON element value types. Thus there is StringSerializer defined for string values serialization. When Mongo tries to serialize document you are inserting, it searches for document type serializer. And finds StringSerializer. But this serializer should be used for string elements serialization, not for documents and you see exception.

Next case is inserting string list as single document:

collection.Insert(list);

In this case type of passed document is IEnumerable, and there is also appropriate serializer exists, which is used to serialize elements of array type. And again you see exception saying about wrong serialier usage:

BsonSerializationException: Serializer EnumerableSerializer expected serialization options of type ArraySerializationOptions, not DocumentSerializationOptions.

And last one error is simple

collection.Save(list);

This method can be used only with documents which have Id field. Before inserting or updating document Mongo tries to get Id value. But neither string nor list of string don't have Id. And you see "No IdGenerator found" exception.

like image 90
Sergey Berezovskiy Avatar answered Nov 15 '22 05:11

Sergey Berezovskiy