Posts

mongoDB Aggregations: Query and Make developers' life easier...

Image
When working with mongoDB aggregations, there will be some situations that you have to write lengthy and complex aggregations. As an example I wrote following aggregation: Which is bit complex, lengthy and time consumed to write. And when this convert to C# it is another time consuming task. The above Aggregation query was write in C# as bellow: As you can see it is very lengthy. So what is the problem now ? Problem...  Think there is a requirement to modify this query to get some other data, or what if there is a bug ? Debugging and modifying this type of code is really gives you a headache; if you DON'T HAVE the mongoDB Aggregation query script saved! If you don't have the script saved you have to convert the C# query to mongoDB Aggregation which is can do but which consumes too much time ! Solution In order to save time and make life easier save the query when you write this kind of complex query follow the easy steps bellow: 1. Save your query I...

MongoDB Aggregations: $cond in C#

When converting mongoDB $cond ( https://docs.mongodb.com/manual/reference/operator/aggregation/cond/ ) aggregation query some times it seems really complex to write in C#. MongoDB has provided 2 different ways to write $cond statement. 1st Approach: { $cond : { if : < boolean - expression > , then : < true - case > , else : < false - case -> } } or 2nd Approach: { $cond : [ < boolean - expression > , < true - case > , < false - case > ] } In order to reduce the complexity of $cond statement, it is always good to use the 2nd Approach , because it will reduce the number of BsonDocumnets which will require when writing the aggregation in C#. So in the following example, I have used a $redact ( https://docs.mongodb.com/manual/reference/operator/aggregation/redact/ ) which use to drop objects with a condition statement. So if a field in that object does not matched with a given value that object will dropped. MongoDB Aggre...

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...

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

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.