How to find Sitecore Contacts in MongoDB

Fed up of trying to your Contact in 100,000s of Contacts in xDB when you only want see if some data is there. Yeah me too. Here are the easiest way to find your Contacts.

First things first, lets get the Contact’s Id. To do that in your browser access the cookies for your site. You should find a cookie named to the effect of SC_ANALYTICS_GLOBAL_COOKIE. That cookie will have a guid which is the Id of the Contact you are currently viewing the site as. The cookie value also has a bool separated by a pipe which I believe is relating to merging of Contacts (will confirm).

SC_Analytics_global_cookie contact id

Robomongo

You’ll likely we want to see the Contact data as its stored. Best way to do that is accessing MongoDB using a tool like Robomongo. Robomongo lets you see the BSON data directly;

Sitecore Contact in xDB

Sadly its not as simple as copying the Contact Id into the Mongo query. The Id needs to have a particular encoding. Fortunately you can do this pretty simply in C# by converting the guid to bytes and then converting the bytes to a Base64String.


// Id of the Contact in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
string contactId = "50638e2f-43b2-46ee-806d-665f4fb08954";
var guid = Guid.Parse(contactId);
var bytes = guid.ToByteArray();
string encodedId = Convert.ToBase64String(bytes);

Make this easy for me

No one has time to run another solution in Visual studio just to convert some Ids. Therefore I’ve written a .NET Fiddle to do the conversion for you. Pass in your contact Id, run it and it spits out an Id you can use in Robomongo to find your contact. I’ve also included the query to run If you’re feeling particularly lazy.

Parsed contact Id:
L45jULJD7kaAbWZfT7CJVA==

Robomongo query:
db.getCollection('Contacts').find({_id:new BinData(3, 'L45jULJD7kaAbWZfT7CJVA==')})

Much easier than having a separate sln to fire up. You can even Fork it so you can pass in a multiple if you’re looking for a number of Contacts.

Web Api

As the Contact data displayed in the Experience Profile is served via Web Api you can actually make a web request to view the data using the Contact Id.

http:///sitecore/api/ao/v1/contacts/

e.g https://jonathanrobbins.co.uk/sitecore/api/ao/v1/contacts/50638e2f-43b2-46ee-806d-665f4fb08954

Returning you a JSON object of all the data stored in xDB. You can even narrow down to particular xDB Elements you want to see data for.

http:///sitecore/api/ao/v1/contacts//intel/

e.g https://jonathanrobbins.co.uk/sitecore/api/ao/v1/contacts/50638e2f-43b2-46ee-806d-665f4fb08954/intel/sampleorders

Fortunately the Url requires authentication on the Sitecore domain before the data is displayed so not something public users can get their hands on.

Experience Profile

Finally you can go the Experience Profile route for a nice UI to go with it. Even if your Contact is not findable by the great Experience Profile search.. (as it may not be added to the Sitecore Analytics Index yet and the search is massively lame). Just add your Contact ID to the Experience Profile Contact Url to see the data in xDB. This isn’t cached data either as its using Web Api to talk to xDB directly.

http:///sitecore/client/Applications/ExperienceProfile/contact?cid=

And that’s it!

Three easy ways to view to find a particular Contact’s data stored directly in xDB. Saving you from trawling through pages and pages of Contacts in Robomongo trying to guess based on the number fields the Contact has.

I also recommend checking out MongoDb’s tutorial on queries for when you come to do more complex searches. Really helped me understand when it came to some custom reporting I have done against xDB.

3 thoughts on “How to find Sitecore Contacts in MongoDB

  1. Pingback: Verify Contacts and Page Visits in XDB|Sitecore implementaion – Learn Sitecore

Leave a comment