Sample
{rating: 2}
{rating: 3}
{rating: 5}
......
What I am trying to do :
var result=db.movies_pipeline.aggregate([{$group: {_id:null, "result": {$avg:"rating"} } }]).
// output : [ { _id: null, result: 6.713884823964879 } ]
db.movies_pipeline.find({
$expr: {
$lt: [
"rating"
,
result[0].result
]
}
},
{ "rating":"$critic_review.rating"}
)
This gives an error:
Cannot read properties of undefined (reading 'result')
Is is mongo shell not an really programming language?
I have checked the result of aggreation:
-> typeof result
object
How can I read the value I want from this object and use it in another query?
2
Answers
@Charchit Kapoor is absolutely correct! I just want add one more solution for learning purpose - map (callback function) can also help you convert the result to the one you want:
aggregate
function returns a cursor. You need to store the result as an array, by using thetoArray
method. Like this: