Sitecore MongoDB Blog Series: Intro to MongoDB for Sitecore Developers

Posted by

Sitecore integrated MongoDB into its platform to enhance xDB analytics database scalability. What do you need to know to be able to speak “Mongo?” This blog series will introduce you to MongoDB, taking you from the basics to advanced uses. Let’s get started!

A Brief Overview of MongoDB

At a high-level, MongoDB is a NoSQL database born out of the limitations with traditional relational databases. While MongoDB is different from SQL, since it’s a non-relational database, it does use a similar data structure hierarchy:

  • SQL Database == MongoDB Database
  • SQL Table == MongoDB Collection
  • SQL Column == MongoDB Property/Field
  • SQL Row ~= MongoDB Document

MongoDB stores its data as JSON documents. Have you seen a JSON data structure?

{ SKU: 1234, IsActive: true, Price: "9.99" }

This makes reading/updating the data stored in MongoDB easy. Each document is independent. Besides the default document identifier / key “_id”, there are no keys (I’ll cover the exception in a future post).

For a more in-depth intro to MongoDB refer to the official documentation: Introduction to MongoDB

Installing MongoDB

It is very easy to install MongoDB.  There are two ways:

  1. Automatically with Sitecore Instance Manager: Sitecore SIM
  2. Manually

If you choose to install MongoDB manually, follow these steps:

  1. Download the recommended MongoDB version for your version of Sitecore.
  2. Install the Mongo server to the default installation path (C:\Program Files\MongoDB\Server\3.2)
  3. Setup Mongo as a service
    1. Assuming mongo was installed to: “C:\Program Files\MongoDB\Server\3.2”
    2. Create a data folder:
      1. C:\Program Files\MongoDB\Server\3.2\data\db
    3. Create a logs folder:
      1. C:\Program Files\MongoDB\Server\3.2\logs
    4. In admin command prompt, navigate to the MongoDB bin folder
      1. c:
      2. cd C:\Program Files\MongoDB\Server\3.2\bin
    5. Run the following command
      1. mongod --dbpath="C:\Program Files\MongoDB\Server\3.2\data\db" --logpath="C:\Program Files\MongoDB\Server\3.2\logs\log.txt" --install
    6. Start –> run –> type in services.msc
    7. Look for “MongoDB”, click start
    8. Ensure service type is set to startup type = automatic (it should be). This will make mongo start automatically when Windows boots.

For a more in-depth guide to installing MongoDB, refer to the official documentation: Install MongoDB Community Edition on Windows

Sitecore MongoDB Connection Strings

Have you seen the MongoDB connection strings in ConnectionStrings.config?

mongo-connection-strings

  • name="analytics": This is the name of the connection string and how you reference it in code.
  • localhost: MongoDB server name.
  • 27017: (Optional) this is the port MongoDB is setup to listen on (27017 is the default port for MongoDB)
  • habitat_local_analytics: MongoDB database name.

One of the best features of MongoDB is that databases and collections create themselves automatically. When you insert a document into MongoDB and the database or collection does not already exist, MongoDB will create the database, collection, and document automatically based on the connection string. Gone are the days of writing database creation scripts and syncing them across your environments. MongoDB does this automatically. You can see this in action. Connect to MongoDB before running Sitecore for the first time, there are no databases. But browse the site, and like magic – they appear.

Viewing MongoDB Databases

You can view MongoDB data in several ways. Here are my recommendations:

  1. Mongo Shell
  2. MongoDB Compass
    • Official GUI by MongoDB
    • Pricing: Free for development purposes; contact MongoDB for Commercial/Advanced Licenses
    • Robust: Analyze performance, execute queries, and a lot more
    • Download: https://www.mongodb.com/products/compass
    • Documentation: https://docs.mongodb.com/compass/current/
  3. Robomongo

I typically use Robomongo in my day-to-day development because it is the fastest way to view MongoDB data. MongoDB Compass runs a bit too slow to be my go-to, but is a necessity for analyzing performance or identifying queries that could be sped up with an index. I use Mongo Shell for administrative purposes or to leverage maintenance scripts, such as nightly backups. All three of these tools serve me well.

Coming Up:

  • A deeper dive into the out-of-the-box Sitecore MongoDB databases
  • Reading/writing custom MongoDB data
  • Creating custom indexes
  • Maintenance scripts
  • Server set-up

Thanks for stopping by! What was your first experience with MongoDB? Are you ready to go NoSQL? Let me know in the comments.

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 )

Google photo

You are commenting using your Google 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