Thinking about model changes when doing MongoDb Aggregations
When writing mongoDB Aggregation quarries in C# it will write as bellow.
var UserAccount = new UserAccountModel();
var matchEmail = new BsonDocument { { "$match", new BsonDocument { { "Email", Email } } } };
var projectProfiles = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "Profile", "UserAccount.UserProfiles" } } } };
var unwindProfile = new BsonDocument { { "$unwind", "$Profile" } };
var matchCompanyId = new BsonDocument { { "$match", new BsonDocument { { "Profile.UserCompanyID", CompanyId } } } };
The result of the above quarry depends on the model CompanyModel and if it has been changed the aggregation will throw an error.
As an example, UserAccount.UserProfiles change into UserAccount.UserProfileList
To make safe this will not be happen and to get notify when the model has been changed at the compilation level, you can use the following way.
var UserAccount = new UserAccountModel();
var matchEmail = new BsonDocument { { "$match", new BsonDocument { { nameof(UserAccount.Email), Email } } } };
var projectProfiles = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "Profile", string.Format("${0}", nameof(UserAccount.UserProfiles)) } } } };
var unwindProfile = new BsonDocument { { "$unwind", "$Profile" } };
var matchCompanyId = new BsonDocument { { "$match", new BsonDocument { { "Profile.UserCompanyID", CompanyId } } } };
In this level I got the advantage of the nameOf keyword of C#. This will gives you the name of the field that is using. And if the model has been changed, the compiler will throw an error.
var UserAccount = new UserAccountModel();
var matchEmail = new BsonDocument { { "$match", new BsonDocument { { "Email", Email } } } };
var projectProfiles = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "Profile", "UserAccount.UserProfiles" } } } };
var unwindProfile = new BsonDocument { { "$unwind", "$Profile" } };
var matchCompanyId = new BsonDocument { { "$match", new BsonDocument { { "Profile.UserCompanyID", CompanyId } } } };
The result of the above quarry depends on the model CompanyModel and if it has been changed the aggregation will throw an error.
As an example, UserAccount.UserProfiles change into UserAccount.UserProfileList
To make safe this will not be happen and to get notify when the model has been changed at the compilation level, you can use the following way.
var UserAccount = new UserAccountModel();
var matchEmail = new BsonDocument { { "$match", new BsonDocument { { nameof(UserAccount.Email), Email } } } };
var projectProfiles = new BsonDocument { { "$project", new BsonDocument { { "_id", 0 }, { "Profile", string.Format("${0}", nameof(UserAccount.UserProfiles)) } } } };
var unwindProfile = new BsonDocument { { "$unwind", "$Profile" } };
var matchCompanyId = new BsonDocument { { "$match", new BsonDocument { { "Profile.UserCompanyID", CompanyId } } } };
In this level I got the advantage of the nameOf keyword of C#. This will gives you the name of the field that is using. And if the model has been changed, the compiler will throw an error.
Comments
Post a Comment