kertAW
kertAW

Reputation: 263

why is nothing returned when using $match?

Why is nothing returned when using $match?

{
  $lookup: {
    from: 'users',
    as: 'user',
    let: {
      "blogIds": "$blog.id"
    },
    pipeline: [{
        $project: {
          id: 1,
          user_name: 1,
          picture: 1,
          blogs: 1
        },
      },
      {
        $match: {
          $expr: {
            $in: ["$$blogIds", "$blogs"]
          }
        }
      }
    ]
  }
}

If I remove $match and add $addFields, I get the following result:

{
  $lookup: {
    from: 'users',
    as: 'user',
    let: {
      "blogIds": "$blog.id"
    },
    pipeline: [{
        $project: {
          id: 1,
          user_name: 1,
          picture: 1,
          blogs: 1
        },
      },
      {
        $addFields: {
          field: "$$blogIds"
        }
      },
    ]
  }
}

enter image description here

Document example:

enter image description here

Upvotes: 1

Views: 28

Answers (1)

Problem - blog.id is array, but you need only value.

So extract the value and use

Option 1

let: {
    "blogIds": { $arrayElemAt: [ "$blog.id", 0 ] }
}

Option -2

{
    $match: {
      $expr: {
        $in: [ { $arrayElemAt: [ "$$blogIds", 0 ] }, "$blogs"]
      }
    }
}

Upvotes: 3

Related Questions