MongoDB Introduction

MongoDB is an open source NoSQL database, it is one of the most prominent of the document-oriented databases. These databases build on the Key-Value model, allowing the user to store complex documents as the value (In MongoDB’s case, they are stored as Binary JSON documents) and an ObjectId (12 byte identifier) value as the key.
MongoDB can run on Windows, there are 32 and 64 bit editions available. There is also a Chocolatey package available.
Also, you’ll probably want to have a client, in my case I got the C# driver from NuGet.
There is good C# Tutorial available on the MongoDB site, with plenty of code examples.
In my experimentation, I created a C# data object class, and simply passed it to the C# driver, which took care of serializing the data to JSON. You can also use LINQ to query your data.
There are also several third party programs used to provide a GUI for MongoDB, I checked out MongoVUE on Chocolatey, which is a free 14 day trial of their product.

To start MongoDB, I ran this command from a BAT file:
“C:\Program Files\gb.MongoDB\bin\mongod.exe” –dbpath “C:\Program Files\gb.MongoDB\data”

The default port number is 27017.
Navigating to http://localhost:28017/ will open a web-based admin tool.

Here is an example, borrowing from the code samples on the MongoDB site. First, you’ll need to add references to the MongoDB.Bson and MongoDB.Driver DLLs.

using System;
using System.Linq;

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace Mongo
{
    class Program
    {
        public class FootballPlayer
        {
            public ObjectId Id { get; set; }
            public string TeamName { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Position { get; set; }
            public int JerseyNumber { get; set; }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Connecting");
            var connectionString = "mongodb://localhost";
            var client = new MongoClient(connectionString);
            var server = client.GetServer();
            var database = server.GetDatabase("test");
            var collection = database.GetCollection("FootballPlayers");

            // Insert 1st entity
            Console.WriteLine("Inserting");

            var entityMR = new FootballPlayer();
            entityMR.TeamName = "Falcons";
            entityMR.FirstName = "Matt";
            entityMR.LastName = "Ryan";
            entityMR.Position = "QB";
            entityMR.JerseyNumber = 3;

            collection.Insert(entityMR);
            var firstId = entityMR.Id;

            // Insert 2nd entity
            var entityRW = new FootballPlayer();
            entityRW.TeamName = "Falcons";
            entityRW.FirstName = "Roddy";
            entityRW.LastName = "White";
            entityRW.Position = "WR";
            entityRW.JerseyNumber = 84;

            collection.Insert(entityRW);
            var secondId = entityRW.Id;

            // Retrieve 1st entity
            Console.WriteLine("Querying One");
            var query = Query.EQ("_id", firstId);
            var retrievedEntity = collection.FindOne(query);

            // Update - Saves, but only sends the changed data across
            Console.WriteLine("Update");
            var update = Update.Set("JerseyNumber", 2);
            collection.Update(query, update);

            //List all
            Console.WriteLine("Querying All");
            var queryAll = from e in collection.AsQueryable() select e;
            foreach (var player in queryAll)
            {
                Console.WriteLine("ID = " + player.Id.ToString());
                Console.WriteLine("Name = " + player.FirstName + " " + player.LastName);
                Console.WriteLine("Position = " + player.Position);
                Console.WriteLine("Jersey Number = " + player.JerseyNumber.ToString());
                Console.WriteLine("");
            }

            // Remove
            Console.WriteLine("Delete All");
            collection.RemoveAll();

            Console.WriteLine("Completed");
            Console.ReadLine();
        }
    }
}
Advertisements

One Response to MongoDB Introduction

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: