Accessing let variables in merge stage - aggregation pipeline


I am currently having an issue in regards to trying to access properties in my merge stage during my aggregation pipeline.

So I have the following merge stage:

{ $project: {
       test: "$someText"
$merge: {
        into: {db: 'test',coll: 'documentTest'},
        on: '_id',
        let: {text: "$test"},
        whenMatched: [{$set: {
                title: "$$text",
        whenNotMatched: 'insert'

But my realm function which runs the pipelines return the following error:

PlanExecutor error during aggregation :: caused by :: Invalid $set :: caused by :: Use of undefined variable: text

FYI. When creating the pipeline using the aggregation tab on compass, it does not give an error before running the pipeline.

Is there something I am missing?

Hope someone can help :slight_smile:

1 Like