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>();