'I want to assign the output of an aggregate function to a variable in node js
Here is my code:
app.get('/getRecommendation', async (req, res) => {
let test=await movie_data.aggregate([
{
$match: {
"vote_average": { $not: { $type: "string" } },
"vote_count": { $not: { $type: "string" } },
"genres": req.query.genres,
"_id":{$ne:monk.id(req.query._id)}
}
},
{
$project: {
original_title: 1,
vote_average: 1,
vote_count: 1,
overview:1,
release_date:1,
genres:1,
distance: {
$sqrt: {
$add: [
{ $pow: [{ $subtract: [Number(req.query.vote_average), "$vote_average"] }, 2] },
{ $pow: [{ $subtract: [Number(req.query.vote_count), "$vote_count"] }, 2] }
]
}
}
}
},
{
$match: {
distance: { $ne: null }
}
},
{
$sort: { distance: 1 },
},
{
$limit: 5
}
])
console.log(test)
})
The output of console.log(test) is an empty array.
I tried different assigning methods, but none of them works and I made sure to make the assignment in an asynchronous way but the output is still empty.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
