Assuming we have a sparking new Sitecore installation running, and have already invested in the relevant MongoDB infrastructure to keep the solution running 24x7 and so on - how can I make further use of this infrastructure in my solution?

I realise that it would probably need to involve something like the C# and .NET MongoDB Driver for very low level access to MongoDB, but are there other and more appropriate resources available?

How would a "Hello World! from MongoDB" example look, when wired up to a Sitecore solution? Including registering the necessary Api Controllers and so on?

It would involve basic CRUD operations.

  • 1
    I've been thinking about this but not got as far as writing code, I'll just point you to the library I've been looking to use here github.com/StackExchange/StackExchange.Redis - written by Mark Gravell of StackOverflow, it's what they use internally. Full details here blog.marcgravell.com/2014/03/… Commented Oct 17, 2016 at 11:30
  • I just now noticed that you mentioned Web API controllers in your question. I am not sure how that is related to accessing MongoDB data. WebAPI can work well with objects coming from any data store. If you have a WebAPI-specific question, I would suggest that you consider asking it separately. Commented Oct 17, 2016 at 18:46

2 Answers 2


Utilizing data extension points

The first thing I would consider is whether your data can be stored as part of the standard Sitecore entities.


The xDB contact has the following fields where you can store custom data that will be saved to MongoDB:

  • Facets
  • Attachments
  • Tags


  • CustomValues

If you use any of the above, you won't have to implement any MongoDB-specific code, as xDB will store and retrieve data for you.

Raw database access

xDB provides the MongoDbDriver class (located in Sitecore.Analytics.Data.DataAccess.MongoDb) that allows you to read and write to MongoDB directly. This is a part of the data access layer used by xDB itself.

Here's how you use it. First, get an instance of the driver:

var driver = MongoDbDriver.FromConnectionString("analytics");

You can also set up your own MongoDB connection string rather than using "analytics".

Then you get a collection instance:

MongoDbCollection collection = driver["YourCollectionName"];

Since MongoDB is schemaless, you can just work with a non-existing collection. It will be created on first access.

Once you have a collection object, you can do CRUD, as well as some extra data and schema operations.

Fetching data

MongoCursor<YourCustomData> cursor =
        Query.LTE("CreatedDate", DateTime.Now));

IList<YourCustomData> results = cursor.Skip(5).Take(10).ToList();

You can also use lambda expressions to refer to fields:

MongoCursor<YourCustomData> cursor =
        Query<YourCustomData>.EQ(_ => _.SomeField, "FieldValue"));

Inserting data

var data = new YourCustomData { SomeField = "SomeValue" };


Creating an index

collection.EnsureIndex(true, "DataFieldName");


If you're fine with CRUD only, you can use a convenience wrapper around the MongoDbCollection.

MongoDbCollection collection = MongoDbDriver.FromConnectionString(connectionStringName)["CollectionName"];

var storage = new MongoDbStorage<YourCustomData, CustomIdType>(collection);

MongoDbStorage has the following methods:

  • Insert
  • Save
  • Update
  • Delete
  • Load
  • Count

Registering custom types in the driver

Note that the driver will serialize/deserialize data for you. But first, you will need to register your types using the helper methods provided by xDB:


You can also specify the collection name to use and the way to get the identifier of your custom objects (that's what will be stored in _id):

MongoDbObjectMapper.Instance.RegisterModelExtension<YourCustomData, CustomIdType>("CollectionName", d => d.CustomId);

Registering custom types is something that should be done once per application lifetime. You can register your types in a static constructor or in a custom initialize pipeline processor.


The answer to that would be, of course you can, just make sure you are not overloading your mongoDB cluster with too much reads and writes. Default Sitecore, the mongoDB cluster will not be loaded up that much so it is possible to use it for other appliances as well.

I would even recommend using it rather then setting up a new resultset. If you want your cluster to have multiple functions and don't know for sure if this will dangr your base functionality, you can choose a node to have another function. Therefor 1 node will only be used by a single instance/programm/function and will never let your cluster suffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.