I'm trying to store the total number of records input to the pipe so I can use the number in a later calculation. How do I grab the number of inputs, then unwind, then use the number of inputs later in my calcs?
I can get the number by doing this:
db.articles.aggregate([
{
$count: "totalArticles"
}
]}
I can get the rest of the data I want by doing this:
db.articles.aggregate([
{
$unwind: "$concepts"
},
{
$group: {
_id: "$concepts.text",
count: {
$sum: 1
},
average: {
$avg: "$concepts.relevance"
},
}
}
])
What I'd really like to do is this:
db.articles.aggregate([
{
$count: "totalArticles"
},
{
$unwind: "$concepts"
},
{
$group: {
_id: "$concepts.text",
count: {
$sum: 1
},
average: {
$avg: "$concepts.relevance"
}
}
},
{
$project: {
count: "$count",
percent: {
$divide: [ "$count", "$totalArticles" ]
}
}
},
{
$sort: {
count: -1
}
}
])