BsonSerializationException when serializing a Dictionary to BSON

The problem is that the new driver serializes dictionaries as a document by default. The MongoDB C# driver has 3 ways to serialize a dictionary: Document, ArrayOfArrays & ArrayOfDocuments (more on that in the docs). When it serializes as a document the dictionary keys are the names of the BSON element which has some limitations … Read more

MongoDB and C#: Case insensitive search

The simplest and safest way to do that is using Linq: var names = namesCollection.AsQueryable().Where(name => name.FirstName.ToLower().Contains(“hamster”)); As explained in the tutorial ToLower, ToLowerInvariant, ToUpper and ToUpperInvariant all perform matches in a case insensitive way. After that you can use all the supported string methods like Contains or StartsWith. This example will generate: { “FirstName” … Read more

How to find min value in mongodb

You can use a combination of sort and limit to emulate min: > db.foo.insert({a: 1}) > db.foo.insert({a: 2}) > db.foo.insert({a: 3}) > db.foo.find().sort({a: 1}).limit(1) { “_id” : ObjectId(“4df8d4a5957c623adae2ab7e”), “a” : 1 } sort({a: 1}) is an ascending (minimum-first) sort on the a field, and we then only return the first document, which will be the … Read more

MongoDB C# 2.0 TimeoutException

This post may help: I figured it out. This JIRA ticket has the details. Effectively, we’ve made a distinction between connecting to a standalone server and connecting directly to a replica set member, where the latter is relatively uncommon. Unfortunately, MongoLab’s Single-Node settings are actually a single node replica set and this causes us to … Read more

Is there an “Explain Query” for MongoDB Linq?

You can get the Json easily enough if you have a query wrapper; var qLinq = Query<T>.Where(x => x.name==”jim”); Console.WriteLine(qLinq.ToJson()); There’s also an Explain() method on MongoCursor, so you could do this; var exp = Collection.FindAs<T>(qLinq).Explain() Console.WriteLine(exp.ToJson()); So if you want the time taken, “millis” is in there; var msTaken = exp.First(x => x.Name == … Read more

Convert MongoDB BsonDocument to valid JSON in C#

MongoDB.Bson (2.5+) has support to map between BsonValues and .Net objects. BsonTypeMapper Class To map a BsonValue (or BsonDocument) to .Net object use var dotNetObj = BsonTypeMapper.MapToDotNetValue(bsonDoc); You can then use your choice of serialization library. For example, JsonConvert.SerializeObject(dotNetObj); If you have a List of BsonDocument var dotNetObjList = bsonDocList.ConvertAll(BsonTypeMapper.MapToDotNetValue);

MongoDB .Net driver 2.0 Pull (remove element)

When using a filter to remove array elements, you need to use the PullFilter builder instead of Pull (which matches whole elements). var collection = db.GetCollection<Person>(“people”); var filter = new BsonDocument(“username”, “bodrum”); var update = Builders<Person>.Update.PullFilter(“followerList”, Builders<Follower>.Filter.Eq(“follower”, “fethiye”)); var result = collection.FindOneAndUpdateAsync(filter, update).Result; Or somewhat more succinctly, using lambdas: var update = Builders<Person>.Update.PullFilter(p => p.followerList, … Read more