RavenDB Document Database – Super Quickstart Guide

Raven-info0RavenDB is among the many available document databases on the market today (mongo, redis, etc.). If you’re looking to try this db and see if it’s right for your project, below is a super quick guide to start using using RavenDB on Windows with asp.net. Below are my 10 simple steps to start using. You can skip to the bottom for entire “source” code. 

  1. Download / install the server from http://ravendb.net/download. In my examples, I left the server port at 8080. After the install is finished and successful, you can browse to: http://localhost:8080 and see the slick management tool.
  2. In Visual Studio, start a new project (asp.net empty project is fine).
  3. Now run Install-Package RavenDB.Client -Version 2.5.2750 in Package Manager Console (pgm). This is a nuget package. This installs the libraries needed to connect to the RavenDB server (installed in step 1).
  4. Create a new web form, default.aspx is fine.
  5. Add a new object (class) to define your document. This was mine (original, I know.):
    public class Person
    {
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
    public int Age { get; set; }
    public bool Handsome { get; set; }
    }
  6. Add “using Raven.Client.Document;” to the top of your code file.
  7. Add these methods to your class:
    public DocumentStore docStore = new DocumentStore();
    public void initDocStore()
    {
    docStore = new DocumentStore { Url = "http://localhost:8080/" };
    docStore.Initialize();
    }
  8. Add a button (and a label for testing) to save some data, add this to button code:
    initDocStore();
    using (var docSession = docStore.OpenSession())
    {
    //new object
    var personToAdd = new Person { FirstName = "Chris", LastName = "Bitting", Age = 21, DOB = Convert.ToDateTime("1/1/2000"), Handsome = false };
    //store object - don't disagree about my age
    docSession.Store(personToAdd);
    //save object
    docSession.SaveChanges();
    //show your new id:
    Label1.Text = "Added: " + personToAdd.Id;
    }
  9. Add a button to get some records:
    initDocStore();
    using (var docSession = docStore.OpenSession())
    {
    var people = from persons in docSession.Query<Person>()
    where persons.FirstName == "Chris"
    orderby persons.Age
    select persons;
    string testOutput = "";
    foreach (Person human in people)
    {
    testOutput = testOutput + human.FirstName + " " + human.LastName + " Age: " + human.Age.ToString() + "<br/>";
    }
    Label1.Text = testOutput;
    }
  10.  That’s it, and just the start!

As a parting note, when wanting to retrieve one record via ID or update records, use methods like below:

//get a single record:
 initDocStore();
 using (var docSession = docStore.OpenSession())
 {
 var human = docSession.Load<Person>("people/1");
//maybe even make some changes
 human.Age = 69;
 //save our changes
 docSession.SaveChanges();
Label1.Text = "Saved!";
 }

Below is the entire source from this guide, enjoy:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="ravendbTest._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
        <asp:Button ID="btnGet" runat="server" OnClick="btnGet_Click" Text="Retrieve" />
        <asp:Button ID="btnSingle" runat="server" OnClick="btnSingle_Click" Text="Get Single Rec" />
    </form>
</body>
</html>

And the code behind:

using System;
using System.Linq;
using Raven.Client.Document;

namespace ravendbTest
{
    public class Person
    {
        public string Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public DateTime DOB { get; set; }

        public int Age { get; set; }

        public bool Handsome { get; set; }
    }

    public partial class _default : System.Web.UI.Page
    {
        public DocumentStore docStore = new DocumentStore();

        public void initDocStore()
        {
            docStore = new DocumentStore { Url = "http://localhost:8080/" };
            docStore.Initialize();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initDocStore();

            using (var docSession = docStore.OpenSession())
            {
                //new object
                var personToAdd = new Person { FirstName = "Chris", LastName = "Bitting", Age = 21, DOB = Convert.ToDateTime("1/1/2000"), Handsome = false };

                //store object
                docSession.Store(personToAdd);

                //save object
                docSession.SaveChanges();

                //show your new id:
                Label1.Text = "Added: " + personToAdd.Id;
            }
        }

        protected void btnGet_Click(object sender, EventArgs e)
        {
            //get some records:
            initDocStore();

            using (var docSession = docStore.OpenSession())
            {
                var people = from persons in docSession.Query<Person>()
                             where persons.FirstName == "Chris"
                             orderby persons.Age
                             select persons;

                string testOutput = "";
                foreach (Person human in people)
                {
                    testOutput = testOutput + human.FirstName + " " + human.LastName + " Age: " + human.Age.ToString() + "<br/>";
                }
                Label1.Text = testOutput;
            }
        }

        protected void btnSingle_Click(object sender, EventArgs e)
        {
            //get a single record:
            initDocStore();

            using (var docSession = docStore.OpenSession())
            {
                var human = docSession.Load<Person>("people/1");

                //maybe even make some changes
                human.Age = 69;

                //save our changes
                docSession.SaveChanges();

                Label1.Text = "Saved!";
            }
        }
    }
}
RavenDB Document Database – Super Quickstart Guide

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 )

Connecting to %s