I would like to create an Aggregation on my data to get the total number of projects with specific tags for a collection of projects in my .Net application.
let type = 1
let dataPipeline = [];
dataPipeline = dataPipeline.concat([{
"$match": {
$and: [{
"basicDetails.isDeleted": 0
},
{
"basicDetails.type": type
}
]
},
},
{
"$lookup": {
from: "ProjectStatusTheme",
localField: "id",
foreignField: "basicDetails.status.id",
as: "statusColorMapping"
}
},
{
"$facet": {
"totalProjectsCount": [{
"$group": {
"_id": {
"Status": {
"id": -1,
"name": "Total Projects",
},
"Color": {
"$arrayElemAt": ["$statusColorMapping.color", {
"$indexOfArray": ["$statusColorMapping.id", -1]
}]
},
},
"count": {
"$sum": 1
}
}
}],
"countByStatus": [{
"$group": {
"_id": {
"Status": "$basicDetails.status",
"Color": {
"$arrayElemAt": ["$statusColorMapping.color", {
"$indexOfArray": ["$statusColorMapping.id", "$basicDetails.status.id"]
}]
}
},
"count": {
"$sum": 1
},
}
},
{
$sort: {
_id: 1
}
}
]
}
}
]);
I want to run the above query in .Net using the Aggregate Framework in MongoDB C# driver. I am able to run the match and lookup in .net as follows:
var match1 = new BsonDocument {
{
"$match",
new BsonDocument {
{
"basicDetails.isDeleted",
0
}, {
"basicDetails.type",
Int16.Parse(type)
}
}
}
};
var lookup1 = new BsonDocument {
{
"$lookup",
new BsonDocument {
{
"from",
"ProjectStatusTheme"
}, {
"localField",
"id"
}, {
"foreignField",
"basicDetails.status.id"
}, {
"as",
"statusColorMapping"
}
}
}
};
var pipeline = new [] {
match1,
lookup1
};
var result2 = projectsCollection.Aggregate < BsonDocument > (pipeline);
However, I am unable to convert $facet to the .NET C# Driver for Mongo. How can I do this using the .NET C# driver?