Using MongoDB from F#

By Kevin Ashton • June 8, 2014
Tags: F#,MongoDB

Let me start off by saying that I have read many arguments against using MongoDB (or in fact any other NoSQL database), typically from the SQL diehards (and have heard those same people call the users of NoSQL databases inexperienced and / or stupid). Some of the arguments are valid criticisms of the general use NoSQL databases, whereas some of them seem like ad-hominem attacks based purely on a lack of knowledge. I don’t consider myself to be inexperienced as I have been developing software that works in a variety of industries for quite a few years now, using all manner of SQL databases (Paradox / BDE, SQL Server, MySQL and Postgres to name a few) and I am familiar with the benefits of these databases, but I also have my own share of complaints about them. Personally however I feel that those that are completely against NoSQL databases such as MongoDB tend to throw out the baby with the bathwater. While the benefits of ACID transactions are not in doubt, I find that there is a certain beauty in the way MongoDB works, particularly in terms of the schema-less design. When combined with F#, MongoDB allows for extremely rapid iterations. I can’t claim to be familiar enough with MongoDB to run it in a production environment, nor have I run into all of the issues that many of the people who have complained about it mentioned. However, for prototyping MongoDB gives me iteration cycles and feedback loops that are at least an order of magnitude faster than I would have were I to be using a traditional SQL database. This decrease in feedback time has lead to me be able to create a system in about a 10th of the time that it would have taken with a traditional relational database, as well as saved me a good number of grey hairs that tend to come about because of schema versioning issues.

With that in mind, I thought I would show how I was using MongoDB from F#. There are a few libraries that attempt to help with making MongoDB more pleasant to work with from F#, but I have written my own which primarily focuses on serialization of F# types (currently Records, Discriminated Unions, Lists, Sets and Maps) to store them in MongoDB. The library I wrote can be found on Nuget (NamelessInteractive.FSharp.MongoDB). Usage of the library is quite simple and requires minimal setup. Install the package, and run two lines to register the serializers and the conventions as follows:

NamelessInteractive.FSharp.MongoDB.SerializationProviderModule.Register()
NamelessInteractive.FSharp.MongoDB.Conventions.ConventionsModule.Register()

Once registered, usage is very simple. Go through the typical setup of your database and collection, for example in the case of my tests it looks like:

let connectionString = "mongodb://localhost"
let client = new MongoDB.Driver.MongoClient(connectionString)
let server = client.GetServer()
let database = server.GetDatabase("TestNamelessFSharpMongo")
let collection = database.GetCollection("RecordTest")
collection.RemoveAll() |> ignore
collection.Insert(testCase) |> ignore
let saved = collection.FindOne()

That's all there is to it. The source code for this library can be found on GitHub here.