How do I perform the SQL JOIN equivalent in MongoDB?

Technology CommunityCategory: MongoDBHow do I perform the SQL JOIN equivalent in MongoDB?
VietMX Staff asked 3 years ago

Mongo is not a relational database, and the devs are being careful to recommend specific use cases for $lookup, but at least as of 3.2 doing join is now possible with MongoDB. The new $lookup operator added to the aggregation pipeline is essentially identical to a left outer join:

{
   $lookup:
     {
       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>
     }
}