Getting started with NoSQL and Raven DB

I started playing around with Raven DB today. Raven DB is a document database, also knows as non-relational, cloud or NoSQL database. It stores schema-less JSON documents/objects (nice).

Raven is an Open Source (with a commercial option) document database for the .NET/Windows platform. Raven offers a flexible data model design to fit the needs of real world systems. Raven stores schema-less JSON documents, allow you to define indexes using Linq queries and focus on low latency and high performance.

Time for a little documentation on my first steps towards NoSQL guruship. Let’s install Raven DB first. We need to download the latest build in a zip file. Unzip it somewhere and start Raven by executing Start.cmd.

The Raven DB command window and Studio start up. By default, Raven DB is listening on port 8080.

One empty index is created by default as you can see here in Raven Studio.

Let’s fire up Visual Studio and create a new Console Application. To add Raven DB functionality to our project, we need to add a reference to Raven.Client.Lightweight.dll found in the RavenDB/Client directory.

To store and Load documents/objects from the database, we simply add code like the one below. Given a Person class we add Friends to the persons friend list and store it in the database. Later we retrieve and output the contents of the Name, Age and Friends properties.

using System;
using System.Collections.Generic;
using Raven.Client;
using Raven.Client.Document;

namespace RavenDbPlayground
{
    class Program
    {
        static void Main(string[] args)
        {
            var store =
                new DocumentStore { Url = "http://l040:8080" };
            store.Initialize();

            using (IDocumentSession session = store.OpenSession())
            {
                StorePerson(session);
                LoadPerson(session);
            }
        }

        private static void StorePerson(IDocumentSession session)
        {
            Person leon = new Person { Name = "Leon", Age = 42 };
            Person manon = new Person { Name = "Manon", Age = 40 };

            session.Store(leon);
            session.Store(manon);

            leon.Friends.Add(manon.Id);
            manon.Friends.Add(leon.Id);

            session.Store(leon);
            session.Store(manon);

            session.SaveChanges();
        }

        private static void LoadPerson(IDocumentSession session)
        {
            Person leon = session.Load<Person>("people/1");
            Console.WriteLine(
                "Person: {0}, age {1}", leon.Name, leon.Age);

            foreach (var friendId in leon.Friends)
            {
                Person friend = session.Load<Person>(friendId);
                Console.WriteLine(
                    "Friend: {0}, age {1}", friend.Name, friend.Age);
            }           

            session.SaveChanges();
            Console.ReadLine();
        }
    }

    public class Person
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public List<string> Friends { get; set; }

        public Person()
        {
            Friends = new List<string>();
        }
    }
}

Et voila :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>