February 26, 2016 by Olga Kogan

What to Expect when Using Mongo Query to get Sitecore Profile Data in .NET

After configuring the persona tool to handle the way profile data is handled by Mongo and creating and populating a mock collection of visit data using .NET driver for Mongo, I saw that the continued development of the Persona Tool required using MongoDB Query to get Sitecore profile data in .NET. The excellent blog post MongoDB C# Driver Cheat Sheet  by my esteemed colleague Derek Hunziker was my "Go to" when working with Mongo Query for .NET.

MongoDB.Driver.Builders namespace offers a lot of functionality to work with Mongo data. The one we are particularly interested in here is a class called Query. When opening the Object Browser we see a plethora of operators.

Persona Tool Mongo Query

In the code snippet below I am using .And (I am combining all filters within that And), .GTE (">="), .LTE ("<="), and .Exists.

"Profiles.Audience Segment". This is quite intuitive and would not intimidate somebody familiar with SQL and/or Linq. However, I ran into some difficulties querying Sitecore xDB profile data. It just so happens that Sitecore's schema for profiles is set up like this: Profile is not a json array of objects but rather an object in itself, as shown I the image below. I erroneously assumed that I could query for Audience Segment by doing something like "Profiles[0]" or "Profiles["Audience Segment"]" but neither of those worked. In order to query successfully I needed "Profiles.Audience Segment".

Sitecore Audience Segment

I passed a profile name (i.e. "Audience Segment") along with a date range to my method that reads visit data for this profile. Once I got the collection I built the string mentioned above - currentProfileString = "Profiles." + profileName which comes out as "Profiles.Audience Segment".

Then I constructed the query object. I filtered by StartDateTime and EndDateTime.

Persona Tool 4 Code A

'Query.Exists(currentProfileString)' is equivalent to '.Where(x => x.Profiles != null && x.Profiles.ContainsKey(profileName))' in Linq.

Human translation: I asked the database to return visit data for documents where profile node "Audience Segment" existed AND where visit start date was within a certain range.

It's very informative to look inside the VisitData class offered by Sitecore.Analytics. From there this can be turned into a custom object, serialized or used as is. Below I have highlighted Profiles (essentially the profile data) and Value (which is actually an Engagement Value.)

Persona Tool Visit Data

In my next blog post I'll discuss how exceptional, extraordinary and essential Engagement Value is, and how Persona Tool analyzes this important piece of information from each visit. If you'd like more information about persona development and marketing strategy, please reach out to Hedgehog Digital Marketing Innovation Team

Keep in Touch and Stay Informed

Get updates, industry reports, white papers and more Hedgehog love.