'Multiple joins trough array field

noSQL beginner here.

For a current use case we are syncing an external relational DB (dataverse) with a changing schema to an mongoDB instance. Because we have no control over the schema and each entity on his one can change we aren't able to normalize the data leading me to the need for nested lookups from an array.

i have created an sample playground to illustrate an rough example of the data structure: https://mongoplayground.net/p/ObWOC5choPq

I need to return the data in roughly the following format:

{
    orderID,
    products: [
        { 
          productID,
          pictureURL,
        },
        { 
          productID,
          pictureURL,
        }
       ]
}

I'm able to perform both lookups seperatly but aren't able to return the picture data as part of an product object in the products array. Could anyone point me in the right direction?

Kind regards,

Nomis



Solution 1:[1]

Maybe something like this:

 db.orders.aggregate([
 {
 "$lookup": {
   "from": "products",
   "localField": "products.productID",
   "foreignField": "_id",
   "as": "products"
  }
  },
  {
   $unwind: {
    path: "$products",
    preserveNullAndEmptyArrays: true
  }
  },
  {
   "$lookup": {
    "from": "pictures",
    "localField": "products.pictureID",
    "foreignField": "_id",
    "as": "products.pictures"
   }
   },
   {
    $unwind: {
      path:"$products.pictures",
      preserveNullAndEmptyArrays: true
    }
   },
   {
   $project: {
    _id: 1,
     products: {
     productID: "$products._id",
     pictureURL: "$products.pictures.bloburl"
    }
   }
  },
  {
   $group: {
   _id: "$_id",
    products: {
    $push: "$products"
   }
  }
  },
  {
   $project: {
   orderID: "$_id",
   products: 1,
   _id: 0
   }
   }
  ])
  1. $lookup orders with products
  2. unwind to convert products to object
  3. $lookup orders with pictures to nested in products
  4. $unwind pictures to object
  5. $project the fields as expected in the final result
  6. $group by order_id.
  7. $project one more time to rename order _id to orders

playground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1