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

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 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

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

Aggregate $lookup with C#

There is no need to parse the JSON. Everything here can actually be done directly with either LINQ or the Aggregate Fluent interfaces. Just using some demonstration classes because the question does not really give much to go on. Setup Basically we have two collections here, being entities { “_id” : ObjectId(“5b08ceb40a8a7614c70a5710”), “name” : “A” … Read more

MongoDB GridFs with C#, how to store files such as images?

Following example show how to save file and read back from gridfs(using official mongodb driver): var server = MongoServer.Create(“mongodb://localhost:27020”); var database = server.GetDatabase(“tesdb”); var fileName = “D:\\Untitled.png”; var newFileName = “D:\\new_Untitled.png”; using (var fs = new FileStream(fileName, FileMode.Open)) { var gridFsInfo = database.GridFS.Upload(fs, fileName); var fileId = gridFsInfo.Id; ObjectId oid= new ObjectId(fileId); var file = … Read more

tech