One last document cross-reference approach we present is to use the aggregation pipeline $lookup stage operator (https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/#lookup-aggregation). This operator performs the equivalent of an SQL LEFT OUTER JOIN. The prerequisite for this operation is to have a field that is common between the two collections referenced. The generic syntax is as follows:
db.<this_collection>.aggregate([
{ $lookup: {
from: <target_collection_name>,
localField: <common_field_in_this_collection>,
foreignField: <common_field_in_target_collection>,
as: <output_array_field> } }
]);
Two other options are available for more complex processing. let allows you to assign a value to a variable for temporary use in the pipeline. pipeline lets you run pipeline operations on the incoming data stream from the target collection. A practical example...