|
To date I have resolved this by simply having the $lookup use the same field name for both from and as keys. This replaces the reference with an array of objects matching the reference, often used with projection. As an example:
db.foo.aggregate([
|
{"$group": {"_id": "$division", "latest": {"$first": "$invoice"}},
|
{"$lookup": {"localField": "latest", "from": "Invoices", "foreignField": "_id", "as": "latest"}},
|
{"$unwind": "$latest"}
|
])
|
The immediate unwind step is a fairly common pattern, too.
|
|
A possible workaround for this: SERVER-18966 ("allow exclusion in $project stage")
|
|
No, sorry for my poor explanation:
In your example:
db.foo.aggregate([
{$lookup: {
from: "bar",
localField: "bar_id",
foreignField: "_id",
excludeLocalField: true,
as: "bars"
}}
])
if the $lookup stage had included excludeLocalField: true (instead of false) then remove referenced localField ("bar_id" in your example)
|
|
Oh, I'm sorry. You want it to remove all local fields? Can you provide an example of what you mean?
|
|
Yes, its correct (but I think that <excludeLocalField: true> must hide/remove local fields)
|
|
jalvarez_madrigal@hotmail.com, If I understand the request correctly, you're looking for this change in behavior:
Example Schema
| test.foo |
test.bar |
{_id: 0, bar_id: 1}
|
{_id: 1, bar_id: 42}
|
|
|
Example Pipeline
db.foo.aggregate([
|
{$lookup: {
|
from: "bar",
|
localField: "bar_id",
|
foreignField: "_id",
|
as: "bars"
|
}}
|
])
|
Example Results, current behavior
{_id: 0, bar_id: 1, bars: [{_id: 1}]}
|
{_id: 1, bar_id: 42, bars: [{_id: 42}]}
|
Example Results, desired behavior
{_id: 0, bars: [{_id: 1}]}
|
{_id: 1, bars: [{_id: 42}]}
|
If the $lookup stage had included excludeLocalField: false, then the output would match the current behavior? Can you confirm?
|
Generated at Thu Feb 08 04:01:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.