Baffled by $lookup results - SOLVED

Consider two collections, widget and gadget.

The schema for widget is as follows:
{“name”:STRING, “reports”:STRING}
** note: “reports” is a string, NOT an array.

And the schema for gadget is as follows:
{“documents”:{“report”:“STRING”, …}, …“neededField”:STRING}

What I want is a new document that looks like:
{“name”:STRING, “reports”:STRING, “gadget”:{…, “neededField”:STRING}}

In other words, I want to join gadgets to widgets based on this relationship:
widget.reports == gadget.documents.report

My $lookup statement is as follows:
var lookup = {
$lookup:
{
from: “gadget”,
localField: “reports”,
foreignField: “documents.report”,
as: “gadget”
}
}

When I run db.widget.aggregate(lookup), my resulting documents are plain-jane gadgets, NOT widgets with a gadget field. It may be the case that I realize what silly mistake I’m making by myself when I come back to look at this, but I thought that I would at least look for some confirmation that I AM in fact doing something silly, and maybe a hint or two.

Oh wow. What I thought was happening wasn’t what was happening at all. Different problem now.

Since you’ve set the title to “Solved”, it’s probably a good idea to show the solution and mark that post as the solution.

1 Like

So the solution was thus:

I thought that as I was scrolling through the output of the aggregation().pretty() that what I was looking at was a collection of gadgets. But what I was actually looking at was a very long array of gadgets within a widget document. In other words - exactly what I expected to see.