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" } };

var pipeline = new[] { matchCompanyId, projectRoles, unwindRoles };

var result = collection.Aggregate<BsonDocument>(pipeline).ToList();

Comments

Popular posts from this blog

Deploy Angular 8 app to Azure with Azure DevOps

Apache ActiveMQ 5 with .Net Core 3.1 and C#

Firebase with.Net Core and C#