Details
-
Bug
-
Status: Backlog
-
Critical - P2
-
Resolution: Unresolved
-
3.4.9, 3.6.0-rc0
-
None
-
Query Optimization
-
ALL
-
Description
If an array element is used as the localField for the $lookup aggregation pipe, a $project anywhere else in the pipeline will fail to return results and also take a while to run. There is a workaround by adding a field containing the local element and using that field as the localField:
db.venues.aggregate(
|
|
// Pipeline
|
[
|
// Stage 1
|
{
|
$addFields: {
|
"firstChainId": {
|
"$arrayElemAt": [ "$chainIds", 0 ]
|
}
|
}
|
},
|
|
// Stage 2
|
{
|
$lookup: {
|
"from" : "chains",
|
"localField" : "firstChainId",
|
"foreignField" : "_id",
|
"as" : "venueChain"
|
}
|
},
|
|
// Stage 3
|
{
|
$addFields: {
|
"chainName": "$venueChain.name"
|
}
|
},
|
|
// Stage 4
|
{
|
$project: {
|
n: 1,
|
chainName: 1
|
}
|
},
|
],
|
|
// Options
|
{
|
cursor: {
|
batchSize: 50
|
}
|
}
|
);
|
Attachments
Issue Links
- is related to
-
SERVER-25200 $project computed fields on arrays sets every element of the array -- inconsistent with $set
-
- Closed
-
- related to
-
SERVER-26066 $project should add top-level field to dependencies for nested computed fields
-
- Closed
-