(Edit : this question was edited to better reflect the issue, which might be a little more complicated than the proposed related question.)
Let's say I have these two collections
products
{
  _id: 'AAAA',
  components: [
    { type: 'foo', items: [
      { itemId: 'item1', qty: 2 },
      { itemId: 'item2', qty: 1 }
    ] },
    { type: 'bar', items: [
      { itemId: 'item3', qty: 8 }
    ] }
  ]
}
items
{
  _id: 'item1',
  name: 'Foo Item'
}
{
  _id: 'item2',
  name: 'Bar Item'
}
{
  _id: 'item3',
  name: 'Buz Item'
}
And that I perform this query
db['products'].aggregate([
  { $lookup: { 
    from: 'items',
    localField: 'components.items.itemId',
    foreignField: '_id',
    as: 'componentItems'
  } }
]);
I get this
{
  _id: 'AAAA',
  components: [
    { type: 'foo', items: [
      { itemId: 'item1', qty: 2 },
      { itemId: 'item2', qty: 1 }
    ] }
    { type: 'bar', items: [
      { itemId: 'item3', qty: 8 }
    ] }
  ],
  componentItems: [ ]
}
Why doesn't the aggregation read the local field value? How can I retrieve the foreign document without losing my original document structure?
Edit
I have read the jira issue and seen the proposed answer, however I don't know how this applies. This is not merely an array, but values from an object, inside an array. I am not sure how I can unwind this, and how to put it back together without losing the document structure.
Edit 2
The problem that I have is that I'm not sure how to group the results back together. With this query :
db['products'].aggregate([
  { $unwind: '$components' },
  { $unwind: '$components.items' },
  { $lookup: {
    from: 'items',
    localField: 'components.items.itemId',
    foreignField: '_id',
    as: 'componentsItems'
  } }
]);
I get the "correct" result of
{ "_id" : "AAAA", "components" : { "type" : "foo", "items" : { "itemId" : "item1", "qty" : 2 } }, "componentsItems" : [ { "_id" : "item1", "name" : "Foo Item" } ] }
{ "_id" : "AAAA", "components" : { "type" : "foo", "items" : { "itemId" : "item2", "qty" : 1 } }, "componentsItems" : [ { "_id" : "item2", "name" : "Bar Item" } ] }
{ "_id" : "AAAA", "components" : { "type" : "bar", "items" : { "itemId" : "item3", "qty" : 8 } }, "componentsItems" : [ { "_id" : "item3", "name" : "Buz Item" } ] }
But, while I can unwind components.items, I cannot seem to unto this, as $group complains that
"the group aggregate field name 'components.items' cannot be used because $group's field names cannot contain '.'"
db['products'].aggregate([
  { $unwind: '$components' },
  { $unwind: '$components.items' },
  { $lookup: {
    from: 'items',
    localField: 'components.items.itemId',
    foreignField: '_id',
    as: 'componentsItems'
  } },
  { "$group": {
    "components.type": "$components.type",
    "components.items": { $push: "$components.items" },
    "componentsItems": { $push: "$componentsItems" }
  } },
  { "$group": {
    "_id": "$_id",
    "components": { $push: "$components" },
    "componentsItems": { $push: "$componentsItems" }
  } }
]);
Edit 3
This query is, thus far, the closest that I found, except that components are not grouped back by type.
db['products'].aggregate([
  { $unwind: '$components' },
  { $unwind: '$components.items' },
  { $lookup: {
    from: 'items',
    localField: 'components.items.itemId',
    foreignField: '_id',
    as: 'componentsItems'
  } },
  { $unwind: '$componentsItems' },
  { $group: {
    "_id": "$_id",
    "components": {
      $push: {
        "type": "$components.type",
        "items": "$components.items"
      }
    },
    "componentsItems": { $addToSet: "$componentsItems" }
  } }
]);
Also: I am concerned that using $unwind and $group may affect the order of the components, which should be preserved. AFAIK, MongoDB preserve array order when storing documents. I'd hate for this functionality to be broken by the awkwardness of $lookup.
