MongoDb with Linqpad

MongoDB is a non-relational database for storing objects (or documents), it's a really nice alternative to the traditional database paradigm and allows the storage and serialization of C# classes and properties.

Quick Start Tutorial

If you don't already have a MongoDb installation, I recommend going for the hosted option at (free option available).

You then need to download the official 10gen C# driver:

Finally you should download Linqpad. I really love this app, you can use it as a C# scratchpad to rapidly play with any new libraries or code base and is a great alternative to running a console app. Just edit your code and hit F5 to run the app. It's especially useful when you want to dump out objects structures to the screen visually, in this case MongoDB data. You can download LinqPad at

Super Quickstart

  • – Create a database here
  • – Download the Driver
  • – Download this

Configuring Linqpad

There is a quick configuration element to Linqpad, we have to add the driver DLL reference and namespace:

First add the DLLs:

Now add the namespaces:

Mongolific! Now we are ready to rock and roll!

Here is a simple example to insert a document (an object class) into a table, and then retrieve it again using a simple query.

void Main()
	var server = MongoServer.Create("mongodb://");

	var db = server.GetDatabase("Universe");

	var collection = db.GetCollection<Planet>("Planet");

	var pluto = new Planet()
		Name = "Pluto",
		Colour = "Purple"



	var query = Query.And(Query.EQ("Name", "Pluto"));

	var list = collection.Find(query).ToList();


public class Planet
	public ObjectId  Id { get; set; }
	public string Name { get; set; }
	public string Colour { get; set; }
© 2020 RK Consulting. All rights reserved.