Wednesday, February 1, 2017

Sitecore viewing and querying MongoDB

When working with Sitecore it can beneficial to be able to view the contents of MongoDB. This comes in handy for tasks such as ensuring a user is added to a contact list or perhaps your custom facets are populating with data as expected.

This will require Robomongo, which put simply is graphical management tool for MongoDB. The main feature of this tool that is relevant to this blog post, is the ability to view and query the data. It does a whole lot more, but one interesting feature worth mentioning is that "First MongoDB tool with fully asynchronous, non-blocking UI".

After installing Robomongo, you can then give it a connection to your local MongoDB instance. This is likely to be via the connection string: localhost:27017. Once it loads up you will see all of the available databases in the left pane.


Expand the analytics database for the instance of Sitecore that you are working on. In this case I am working with the contacts, so will be querying on this.


You are now given an empty query that shows all of the contacts for your Sitecore instance.


If I wanted to find a user with a specific email address, the query would be:
db.getCollection('Contacts').find({"Emails.Entries.Preferred.SmtpAddress":"1@email.com"})


You will notice that the query is a dot notation of the strucuture of the MongoDB object and in this case is looking for a string match.


However sometimes, you might want a more dynamic query. Such as all users who have an email address added. The query for this one would be:
db.getCollection('Contacts').find({"Emails.Entries.Preferred.SmtpAddress":{$ne:null}})

These are just a couple of quick examples of how to view and query the contents of your Sitecore MongoDB.

No comments:

Post a Comment