Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-10791

Comment on: "manual/reference/operator/aggregation/lookup"

      Based on server ticket SERVER-22881, the text in $lookup documentation saying that an $unwind is necessary is no longer accurate.

      $unwind may still be used for prettier formatting, but the same can be accomplished on the other side of the $lookup with $project and some other expressions.

      Using the same sample documents in the document example, see the following pipeline and output.

      db.orders.aggregate([
        {
           $lookup:
              {
                 from: "inventory",
                 localField: "specs",
                 foreignField: "size",
                 as: "inventory_docs"
             }
        },
        {
           $match: { "inventory_docs": { $ne: [] } }
        }
      ])
      
      {
      	"_id" : 1,
      	"item" : "MON1003",
      	"price" : 350,
      	"quantity" : 2,
      	"specs" : [
      		"27 inch",
      		"Retina display",
      		"1920x1080"
      	],
      	"type" : "Monitor",
      	"inventory_docs" : [
      		{
      			"_id" : 1,
      			"sku" : "MON1003",
      			"type" : "Monitor",
      			"instock" : 120,
      			"size" : "27 inch",
      			"resolution" : "1920x1080"
      		}
      	]
      }
      

            Assignee:
            kay.kim@mongodb.com Kay Kim (Inactive)
            Reporter:
            nathan.leniz@mongodb.com Nathan Leniz
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              4 years, 42 weeks, 3 days ago