I am using mongodb Aggregate query. My db is like this:
{
"_id" : ObjectId("5a81636f017e441d609283cc"),
"userid": "123",
page : 'A',
newpage: 'A',
},
{
"_id" : ObjectId("5a81636f017e441d609283cd"),
"userid": "123",
page : 'B',
newpage: 'A',
},
{
"_id" : ObjectId("5a81636f017e441d609283ce"),
"userid": "123",
page : 'D',
newpage: 'D',
}
I want to get the Sum of all page and new page value. I am able to get one column value which can give the very precise result.
But I am stuck with the two columns. What I did for getting the sum/repetition of values for one column is:
db.Collection.aggregate([
{$match:{ "userid":"123"}},
{$unwind:"$newpage"},
{$group:{"_id":"$newpage", "count":{"$sum":1}}},
{$project: {_id:0, pagename :"$_id", count:{ $multiply: [ "$count", 1 ] }}},
{$sort: {count: -1}},
//{$limit: 10}
], function(error, data){
if (error) {
console.log(error);
} else {
console.log(data);
}
});
Desired Result will be like:
{
"pagename": "A",
"count": 3
},
{
"pagename": "D",
"count": 2
},
{
"pagename": "B",
"count": 1
}
Is anyone has any approach to getting these things for Two Column? Any Help is appreciated
$facetallows you to process multiple aggregation pipelines within a single stage on the same set of input documents.