RavenDB – Exporting and Importing using Smuggler

badge1Smuggler is a tool for RavenDB which is used for exporting and importing data – today at work I was shown a nice way to use it to take production data out and import it locally for just one collection of documents – something which might be useful and I know I will forget the syntax I the future for so blogging about it helps me know remember it and I can always read here again in the future if and more likely when I do forget it.

The syntax for exporting a collection called Categories would be as follows:-


What are we doing here, well we are saying use Smuggler to export from the Catalogue Tenants database a dump file – we are also using metadata so that we are only exporting a collection of documents called Categories and then only the latest version so no revisions, and lastly only export the Documents so no Indexes or any other types of documents.

I wanted to then import this into my local instance of RavenDB so the following command does just that:-


This will import the Categories and override any existing Categories in my local RavenDB instance.

You can read more about smuggler here.

Hope someone finds that useful.

My DotNetCurry Magazine Articles

Over the last couple of months I have been privileged to write a couple of articles for the free .Net Developer Magazine brought to you by

Its been my first attempt at writing for a magazine and has been a lot of fun, maybe in the future I can write more articles.

Please enjoy and give them a read – and sign up for the magazine – its FREE!

Create a Snappy UI with KnockoutJS -> Issue 3
This article explores KnockoutJS and what KnockoutJS gives you as a developer.

Web Essentials for Visual Studio 2012 -> Issue 4
This article explores the Visual Studio 2012 Web Essential add-on.


What’s New in RavenDB 2.0 -> Issue 5
This article explores RavenDB 2.0

Take a look at these articles and hopefully you will learn something new – Gregor.

RavenDB – Safe By Default

One of RavenDB’s unique features is something called safe by default – this means that the database is configured to stop users querying for large amounts of data which is never a good idea, to have a query return thousands of records is inefficient and can also take a long time.

With RavenDB safe by default limits the number of records by default on the client side to 128 (by default means its configurable) if you don’t make use of Take(). Server side you can use .Take(1024) if you want to go up to the limit configured by the server, if you try to bring back more than 1024 then it will default to 1024 results returned if you need to return more data than that then you should be asking yourself why?, page your data if you need to return more than this number of records.

Safe by default is attempting to stop a developer from writing poor queries, there is nothing to stop you from changing the defaults to a higher level but if you’re looking to display thousands of records on a webpage without paging through your data then this is seen as poor design, here is a nice a quote from the RavenDB website: –

RavenDB will let you shoot yourself in the foot, but only after you make it absolutely clear that this is what you actually want to do

It seems to be quite common for developers to try to work around the limits and actually want to block their leg off, this usually stems from a misunderstanding of how to use RavenDB, it really is a very simple concept which I find to be a nice feature.

RavenDB also had a very useful option to stop yourself from creating the dreaded SELECT N+1 scenario – this feature stops after 30 request to the server per session, again configurable but if you’re hitting this limit then your more than likely querying incorrectly.

To summarise safe by default is there to help and not hinder, yes you’ll write code then realise your only returning 128 records when there should be more results, no doubt everyone has done that whilst using RavenDB, either your unit tests aren’t right or you’ve forgotten to use .Take()

Free RavenDB Talk in Glasgow on November 14th

My colleague at work Mohammed Ibrahim is doing a free talk titled RavenDB: working with NoSQL data in .NET at Glasgow Caledonian University on Nov 14.

If your unfamiliar with noSQL and want to know more then come along and you will learn all about it and more.

So come along, learn, ask some questions and to get an insight into why RavenDB rocks, register at http://ravendbglasgow.eventbrite.com – tickets are selling fast so get yours while you can.

There will be free swag! to give away at the event, including t-shirts and RavenDB stickers.

What I learned last week – 3rd September – 7th September

This past week I have been mainly working with KnockOutJS, if you looking for a way to create a rich UI that users will love and your building a website then I highly recommend you take a look at KnockoutJS

What I learned this past week

  • Web Essentials 1.1 for VS 2012 by Mads Kristensen has been updated, you can read more here – go check it out.
  • RavenDB Backups – I decided to instead of using the RavenDB Backup.exe – I am going to stick with just exporting the tenant database and saving this off to a network share.
  • At work we are looking at introducing CoffeeScript – CoffeeScript is a language that sits on top of JavaScript and compiles down to JavaScript but has added benefits including making your code cleaner and has much more signal to noise ratio – more on CoffeeScript soon , I’m off to learn more on it via PluralSight.
  • Redgate have taken on the 2 developers who write Glimpse and will be adding to the project – you can read more here.

What did you find useful this week? – please let me know by leaving a comment after the beep.

RavenDB – using the uniqueconstraint bundle

During the week at work we came across an issue where we are doing a linq query which is using a statement such as this:-

var product = session.Query().Single(GetSource(key));

In Sql Server land we have a product table and the field productcode is unique – we noticed that in RavenDB we were seeing products
which had the same productcode in different documents (not unique), this was not the desired outcome and so we looked into how to go about fixing this in RavenDB, enter the uniqueconstraints RavenDB bundle.

RavendDB bundles are like add-ons and Raven comes with quite a few and they can be exceptionally handy. To use the uniqueconstraints bundle you create a folder and call it plugins, usually you keep this inside the server folder but you can put it anywhere and add a config value so that Raven can find your plugins, an example of how to set up your config to locate plugins:-

<?xml version="1.0" encoding="utf-8" ?>
    <add key="Raven/Port" value="*"/>
    <add key="Raven/DataDir" value="c:\RavenDB\Data"/>
	<add key="Raven/IndexStoragePath" value="c:\RavenDB\Data\Indexes" />
	<add key="Raven/Esent/LogsPath" value="c:\RavenDB\Data\Logs" />
	<add key="Raven/PluginsDirectory" value="c:\RavenDB\Plugins"/>
    <add key="Raven/AnonymousAccess" value="All"/>
	<add key="Raven/ResetIndexOnUncleanShutdown" value="true"/>
		<loadFromRemoteSources enabled="true"/>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<probing privatePath="Analyzers"/>

Server side checking

  • To to use the uniqeconstraints bundle add the Raven.Bundles.UniqueConstraints.dll from the bundles folder to your plugins folder where ever you decide to put them.
  • Then restart RavenDB
  • To see if they have been installed and picked up – go to the Raven Management Studio and then at the bottom left hand side, click on statistics – next to where it says Triggers it should say something like Raven.Bundles.UniqueConstraints.UniqueConstraintsDeleteTrigger

Client Side checking

In order to implement the check through code we borrowed the example written by Richard Dingwall taken from his blog.

namespace WhateverNameSpaceYouHave.Extensions.RavenUniqueConstraint
    using System;
    using System.Linq.Expressions;

    using Raven.Client;

    public interface IRavenUniqueInserter
        void StoreUnique(IDocumentSession session, T entity, Expression<Func<T, UTunique>>; keyProperty);

Aboove the interface, below that code that uses it:-

namespace WhateverNameSpaceYouHave.Extensions.RavenUniqueConstraint
    using System;
    using System.Linq.Expressions;

    using Raven.Abstractions.Exceptions;
    using Raven.Client;

    public class RavenUniqueInserter : IRavenUniqueInserter
        public void StoreUnique(IDocumentSession session, T entity, Expression<Func<T, Tunique>> keyProperty)
            if (session == null)
                throw new ArgumentNullException("session");

            if (keyProperty == null)
                throw new ArgumentNullException("keyProperty");

            if (entity == null)
                throw new ArgumentNullException("entity");

            var key = keyProperty.Compile().Invoke(entity).ToString();

            var constraint = new UniqueConstraint { Type = typeof(T).Name, Key = key };

            DoStore(session, entity, constraint);

        private static void DoStore(IDocumentSession session, T entity, UniqueConstraint constraint)
            var previousSetting = session.Advanced.UseOptimisticConcurrency;

                session.Advanced.UseOptimisticConcurrency = true;
                session.Store(constraint, string.Format("UniqueConstraints/{0}/{1}", constraint.Type, constraint.Key));
            catch (ConcurrencyException)
                // rollback changes so we can keep using the session
                session.Advanced.UseOptimisticConcurrency = previousSetting;

How to call it from code

                    new RavenUniqueInserter().StoreUnique(session, destination, x =&gt; x.Code);
                    LogManager.GetCurrent().Event(LoggingLevel.Info, "Inserting Product:+" + destination.Code, "Method name here");
                catch (ConcurrencyException)
                        "Product code already in use. Code: " + destination.Code, "Method name here");

And thats it, now we wont be able to add a product with a productcode which already exists in our RavenDB database, making it a unique constraint just as youd have in Sql.

Please add a comment or ask a question if you found this useful.

What I learned last week – July 23-27th

This is the first in a series I am going to do on what I learned last week – it will cover what I’ve been up to and what I have read and learnt throughout the week – ok let’s get started.

Found a really nice sample on how to notify users when an action on your site has taken place – say you save a record or delete a record, perhaps you need to show a validation message, maybe even a banner at the top that has to stay there,

Take a look at the oh so simple to use JQuery plugin called Notify.

Awesome Stuff

  • Visual Studio 2012 is really nice to use! – you should take a look now if you havent already.
  • Shout out to the guys at RavenDb highly recommend it, also note its FREE until you need to release to production, so if you’re doing demos, needing a quick win on a project – take it for a spin.
  • Resharper 7 is out – if you’re not already using this then do yourself a favour, why walk when you can run, go try it now Resharper 7.
  • Redgate have brought out a plugin for setting up a database build in TeamCity Redgate TeamCity Plugin

Please leave a comment after the beep.