$search in $lookup with MongoDB v6

Can anyone help me out with using $search in $lookup?

It works fine if I use a static string instead of searchId, but I’m not able to use a variable from the “let” definition block. Anyone knows how to resolve variables inside the $search function?

1 Like

$lookup variables can only be referenced inside $expr and I don’t believe that’s supported in $search at the moment.

Oh no, that’s a pity. :frowning:

Just to confirm - the question here is for the $search aggregation stage. Is your answer also valid for $search field of the $text operator? For example, a $lookup stage with the following content:

{
  from: "OtherCollection",
  let: {
    otherName: "$name",
  },
  pipeline: [
    {
      $match: {
        $text: {
          $search: "$$otherName",
        },
      },
    },
  ],
  as: "references",
}

This is not expected to work as well, right?

Correct.

$$expr is a top level expression.