mongoDB Aggregations to C#
mongoDB Aggregations to C#
mongoDB Aggregation
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
Post a Comment