2

I am new to MongoDB structure with Sitecore data processing. I want to get contact Id based on EmailId from MongoDB with an query.

  • How to get contact Id from MongoDB based on EmailId?
  • How it will be generated?
  • Where it would be stored?

As sitecore_analytics database has identifiers collection where in I could see id field which has EmailId value.

Each document has below fields in identifiers collection.

Id field has EmailId. Contact field has some GuId. Update field is latest date time value.

If I check contact field of [email protected] email in MongoDB collection, Contact field value is different from actual contact id in Sitecore.

http://localhost/sitecore/client/Applications/ExperienceProfile/contact?cid=xxxxx-xxxx-xxxx-xxxx-xxxxxxx

Thanks & Regards.

2
  • Have you considered using sitecore_analytics search index instead of directly querying MongoDB?
    – grg
    Commented Nov 6, 2018 at 8:43
  • @grg No, How can I Do it? Commented Nov 6, 2018 at 9:43

2 Answers 2

2

I think this will get you what you need.

The model

We need to build a Bson model to push into our query

namespace Sitecore.Feature.Xdb.Models
{
    using MongoDB.Bson.Serialization.Attributes;

    [BsonIgnoreExtraElements]
    public class ContactRecord
    {
        public Identifiers Identifiers { get; set; }

    }

    [BsonIgnoreExtraElements]
    public class Identifiers
    {
        public string Identifier { get; set; }
    }
}

The function

This function will make a call directly into Mongo and find a user whos Identifiers.Identifier equals the incoming email address.

using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

public void FindContactByEmail(string email)
{
    try
    {
        var connectionString = ConnectionString();
        if (string.IsNullOrEmpty(connectionString))
            return;

        MapContactModelClass();

        SearchForEmailIdentifier(InitializeXdbConnection(connectionString), email);
    }
    catch (Exception ex)
    {

    }
}

private static void MapContactModelClass()
{
    if(!BsonClassMap.IsClassMapRegistered(typeof(ContactRecord)))
        BsonClassMap.RegisterClassMap<ContactRecord>(cm => { cm.MapMember(c => c.Identifiers); }); 
}

private static string ConnectionString()
{
    return ConfigurationManager.ConnectionStrings["analytics"]?.ConnectionString;
}

private static MongoCollection<BsonDocument> InitializeXdbConnection(string connectionString)
{
    var client = new MongoDB.Driver.MongoClient(connectionString);
    var passNodeDb = client.GetServer().GetDatabase("sitecore_dev_analytics");

    return passNodeDb?.GetCollection("Contacts");
}

private static void SearchForEmailIdentifier(MongoCollection<BsonDocument> passesCollection, string email)
{
    if (passesCollection == null) return;

    var query = Query<ContactRecord>.EQ(u => u.Identifiers.Identifier, email);

    var swimmerPass = passesCollection.FindOne(query);
    var id = swimmerPass["_id"].AsGuid;

}

You can also use some Sitecore built in functions, but a lot of them are marked as internal, so it is hard to access them.

var driver = MongoDbDriver.FromConnectionString("analytics");
var visitorData = driver.Interactions.FindAllAs<VisitData>();
1

I've not had to dereference a contact ID from an email ID, so I'm not exactly sure how they are related, but I do know you will need to convert the contact ID into the appropriate format for Mongo DB. The easiest way is in code, for which there is a convenient DotNetFiddle:

https://dotnetfiddle.net/T8iijs

// 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();
var encodedId = Convert.ToBase64String(bytes);

// Example: "db.getCollection('Contacts').find({_id:new BinData(3, "L45jULJD7kaAbWZfT7CJVA==')})"
var mongoQuery = "db.getCollection('Contacts').find({" + string.Format("_id:new BinData(3, '{0}')", encodedId) + "})";

The above query will return the contacts with the ID you passed in (should just be one). You can then use that same ID to find any interactions with that ID:

db.getCollection('Interactions').find({
    "ContactId": new BinData(3, 'Wvck3FMkukG3Ex8yUN3z6g==') 
}
1
  • Thankyou but as I have email id as value in id field in collection document, Cannot I query in mongodb collection without code based? Commented Nov 5, 2018 at 15:52

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.