Node.js MongoDB Join

Node.js MongoDB Join (Using $lookup)

MongoDB does not support SQL-style joins, but it has a powerful aggregation operator called $lookup that allows joining two collections.


Example Collections

customers collection:


orders collection:


We will join customers and orders using:


1. Basic Join Example



Output Example



2. Join + Match (LIKE WHERE + JOIN)

Get only customers from Surat with their orders:


3. Join + Unwind (Flatten array)

$unwind converts the joined array into individual documents.


4. Join With Projection (Select Specific Fields)


5. Multiple Joins

You can join more than 2 collections:


6. Full Working Example (Node.js)

 

You may also like...