Hi…I am having this same issue. Can you please tell me what you mean by th requirement to append the new merge stage when required? I have mine in the last stage of the pipeline and it works fine in Studio3T.
// Pipeline
[
// Stage 1
{
$match: {
category: "project",
isTemplate: {$ne: true},
bPortfolio: true,
customerId: "1",
archived: {$exists: false},
deleted: {$exists: false}
}
},
// Stage 2
{
$lookup: // Equality Match
{
from: "tasks", "localField":"_id", foreignField: "project_id", as: "project_tasks"
}
// Uncorrelated Subqueries
// (supported as of MongoDB 3.6)
// {
// from: "<collection to join>",
// let: { <var_1>: <expression>, …, <var_n>: <expression> },
// pipeline: [ <pipeline to execute on the collection to join> ],
// as: "<output array field>"
// }
},
// Stage 3
{
$unwind: {
path : "$project_tasks"
}
},
// Stage 4
{
$match: {
"project_tasks.deleted": {$exists: false},
"project_tasks.dashHide": false
}
},
// Stage 5
{
$project: {
_id: 1, portfolioName: "$name", portfolioStatus: "$status",
portfolioDescription: "$description", portfolioDashHide: "$dashHide", customerId: "$customerId",
locations: "$locations", creator: "$creator", collaborators: "$collaborators",
project_tasks: "$project_tasks",
numberOfAddresses: {$cond: { if: {$isArray: "$project_tasks.addresses" }, then: {$size: "$project_tasks.addresses"}, else: 0}}
}
},
// Stage 6
{
$match: {
numberOfAddresses: {$gt: 0}
}
},
// Stage 7
{
$unwind: {
path : "$project_tasks.addresses"
}
},
// Stage 8
{
$project: {
_id:1, portfolioName:1, portfolioStatus: 1, portfolioDescription: 1, portfolioDashHide: 1, customerId: 1, locations: 1, creator: 1, collaborators: 1,
"addressTask._id": "$project_tasks._id", "addressTask.card_id": "$project_tasks.card_id", "addressTask.dashHide": "$project_tasks.dashHide",
"addressTask.tableDashHide": "$project_tasks.tableDashHide", "addressTask.position": "$project_tasks.position",
"addressTask.color": "$project_tasks.color", "addressTask.name": "$project_tasks.name", "addressTask.markerSize":
"$project_tasks.markerSize", "address.name": "$project_tasks.addresses.name", "address.cleanAddress": "$project_tasks.addresses.cleanAddress",
"address.coordinates": "$project_tasks.addresses.coordinates", "address.addressComponents": "$project_tasks.addresses.addressComponents",
"address.project_id": "$project_tasks.addresses.project_id", "address.collectionId": "$project_tasks.addresses.collectionId",
"address.geoDone": "$project_tasks.addresses.geoDone"}
},
// Stage 9
{
$lookup: // Equality Match
{
from: "projects", localField: "address.project_id", "foreignField": "_id", as: "projectAddress"
}
},
// Stage 10
{
$unwind: {
path : "$projectAddress"
}
},
// Stage 11
{
$project: {
_id: 1, portfolioName: 1, portfolioStatus: 1, portfolioDescription: 1, portfolioDashHide: 1, customerId: 1, locations: 1, creator: 1, collaborators: 1,
addressTask: 1, "address.name": "$address.name", "address.cleanAddress": "$address.cleanAddress",
"address.coordinates": "$address.coordinates", "address.addressComponents": "$address.addressComponents",
"address.project_id": "$address.project_id", "address.collectionId": "$address.collectionId",
"address.geoDone": "$address.geoDone", "address.csvFields": "$projectAddress.csvFields",
"address.csvFieldsArray": "$projectAddress.csvFieldsArray", "address.projectName": "$projectAddress.name"
}
},
// Stage 12
{
$group: {
_id: {portfolioId: "$_id", customerId: "$customerId", portfolioName: "$portfolioName", portfolioStatus: "$portfolioStatus",
locations: "$locations", creator: "$creator", collaborators: "$collaborators",
portfolioDescription: "$portfolioDescription", portfolioDashHide: "$portfolioDashHide", addressTask: "$addressTask"},
"addresses": {$push: "$address"}}
},
// Stage 13
{
$project: {
"_id": "$_id.portfolioId", "name": "$_id.portfolioName", "status": "$_id.portfolioStatus", customerId: "$_id.customerId",
locations: "$_id.locations", creator: "$_id.creator", collaborators: "$_id.collaborators",
"description": "$_id.portfolioDescription", "dashHide": "$_id.portfolioDashHide", "addressTask._id": "$_id.addressTask._id",
"addressTask.card_id": "$_id.addressTask.card_id",
"addressTask.dashHide": "$_id.addressTask.dashHide", "addressTask.tableDashHide": "$_id.addressTask.tableDashHide",
"addressTask.position": "$_id.addressTask.position", "addressTask.color": "$_id.addressTask.color",
"addressTask.name": "$_id.addressTask.name", "addressTask.addresses": "$addresses"}
},
// Stage 14
{
$group: {_id: {"_id": "$_id", customerId: "$customerId", name: "$name", status: "$status", description: "$description", dashHide: "$dashHide",
locations: "$locations", creator: "$creator", collaborators: "$collaborators"},
"addressTasks": {$push: "$addressTask"}}
},
// Stage 15
{
$project: {
_id: "$_id._id", name: "$_id.name", status: "$_id.status", description: "$_id.description", dashHide: "$_id.dashHide",
customerId: "$_id.customerId", locations: "$_id.locations", creator: "$_id.creator", collaborators: "$_id.collaborators", addressTasks: 1
}
},
// Stage 16
{
$merge: {
// The $merge operator must be the last stage in the pipeline
into: "portfolioAddresses",
//on: <identifier field> -or- [ <identifier field1>, ...], // optional
//let: <variables>, // optional
whenMatched: "replace", // optional
whenNotMatched: "insert" // optional
}
},
]