Chapter 3 $lookup lab

Hi.
I had “an output” (including the correct answer) ONLY when All Stages were included in One pipeline of the aggregate call: $match, $lookup, $unwind, $group all were present to get an output. When I had only $match, $lookup so that I would play around with newly added array field “joined” as specified in the ‘as’ field, there was NO added field to any matching docs in air_routes collection; I ran the following:
db.air_routes.aggregate([ {$match: {“joined”: {$exists: true}}}, {$count: “total”} ]), I got nothing.
My question is, does the $lookup stage produce a cursor output that only exists in memory without actually adding a new field to the input collection? I couldn’t see any doc in my input collection that had the ‘as’ field “joined” added to it. However, when I included the 4 pipeline stages ($match, $lookup, $unwind, $group) in One aggregate call, I got an output for the 1st time, which was the correct answer.
Appreciate an explanation as to how $lookup behaves. I couldn’t find anything in the documentation that tackles this issue.
Rhonda

An aggregation pipeline used in db.Collection.aggregate() does not modify the source collection.

If you want to update the source collection with an aggregation you may use https://docs.mongodb.com/manual/tutorial/update-documents-with-aggregation-pipeline/

Thanks for reminding me of that…

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.