Posts

Showing posts from March, 2017

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.

Remove objects from an mongoDB Array

mongoDB Collection {     "_id" : ObjectId("58d0e87a0fa052d188424bc7"),     "_CompanyID" : BinData(3, "+4IlRPd+6UmiuIJ7sNHwfQ=="),     "CompanyName" : "dprsales7200",     "CustomRoles" : [         {             "RoleId" : NumberInt(3),             "Name" : "ss"         },         {             "RoleId" : NumberInt(4),             "Name" : "sssdd"         }     ]     } } Q. I want to remove a Custom Role from the CustomRoles array which matches the RoleID. C# Solution public async Task<byte> deleteCustomRoleToCompany(Guid companyId, IdentityUserRole role)         {             var companyCollection = dbRepo.GetDbCollection().GetCollection<CompanyModel>(DBColletions.CollectionComapny);             var removeFilter = Builders<CompanyModel>.Update.PullFilter(c => c.CustomRoles, f => f.RoleId == role.RoleId);    

Issue with AngularJs filter in ng-repeat

When using a filter inside ng-repeat I found a problem where it behaving strange and showing the same value in two location even I filtered it using javascript logic. Then I found out the problem is with the text of the label. AngularLogic: ng-repeat="item in customRole.userActions | filter:item.ActionGroup='Chat'" What I'm getting from the C#: [Description("Create Ticket From Chat")] Since the description is containing the text 'Chat' it will filter the wrong actions and show them in wrong places.

mongoDB Aggregations to C#

mongoDB Aggregations to C# mongoDB Aggregation  db.UserAccount.aggregate(   // Pipeline   [     // Stage 1     {       $match: {                "UserProfiles.UserCompanyID": BinData(3, "mwOPobw5rtf6XidzEfd4PA==")       }     },     // Stage 2     {       $project: {                Roles: "$UserProfiles.UserRoles"       }     },     // Stage 3     {       $unwind: "$Roles"     }   ] ); C#  var collection = dbRepo.GetDbCollection().GetCollection<BsonDocument>(DBColletions.CollectionUserAccount);                  var matchCompanyId = new BsonDocument { { "$match", new BsonDocument { { "UserProfiles.UserCompanyID", companyId } } } }; var projectRoles = new BsonDocument { { "$project", new BsonDocument { { "Roles", "$UserProfiles.UserRoles" } } } }; var unwindRoles = new BsonDocument { { "$unwind", "$Roles&

Query mongoDB with C# LINQ

Query mongoDB with C# LINQ 1. Project     Get property of the model directly from the database. Eg: var isActive = await collection.Find(a => a.userID == userId). Project (s => s.IsActive).FirstOrDefaultAsync(); without using: var isActive = (await collection.FindAsync(a => a.userID == userId).Result.FirstOrDefaultAsync()).IsActive; which gets the entire model and query it for the property; IsActive.