0
votes

I'm trying to do a regex match inside an aggregation pipeline $lookup

So lets assume the following query:

$lookup: {
   from: 'some-collection',
   let: {
      someIds: '$someIds'
   },
   pipeline: [
      {
         $match: {
            $expr: {
               $and: [
                  {
                     $in: ['$someId', '$$someIds']
                  },
                  {
                     $not: {
                        $eq: ['$status', 'archived']
                     }
                  }
               ]
            }
         }
      }
   ]
}

This all works great, i can match on multiple conditions, and it works.

However if i want to add another condition using an array of regex i can't get it to work

$lookup: {
   from: 'some-collection',
   let: {
      someIds: '$someIds'
   },
   pipeline: [
      {
         $match: {
            $expr: {
               $and: [
                  {
                     $in: ['$someId', '$$someIds']
                  },
                  {
                     $not: {
                        $eq: ['$status', 'archived']
                     }
                  },
                  {
                     $in: ['$some-type', [/type1/, /type2/]]
                  }
               ]
            }
         }
      }
   ]
}

Why does this not work? as i understand it from the documentation i should be able to use regex this way inside an $in operator, and i can confirm that it works, since we use it elsewhere. However nested within a $lookuppipeline it does not.

Is this a bug or am i overlooking something? Is there another way i can do this kind of regex match?

1

1 Answers

1
votes

Evidently, the problem appears to be that i was attempting to regex match inside the $expr operator, im unsure as to why it does not work, and i can't find anything within the documentation.

But by moving it to a seperate match within the pipeline it worked.

$lookup: {
   from: 'some-collection',
   let: {
      someIds: '$someIds'
   },
   pipeline: [
      {
         $match: {
            $expr: {
               $and: [
                  {
                     $in: ['$someId', '$$someIds']
                  },
                  {
                     $not: {
                        $eq: ['$status', 'archived']
                     }
                  }
               ]
            }
         }
      },
      {
         $match: {
            some-type: {
               $in: [/type1/, /type2/]
            }
         }
      }
   ]
}

If anyone can elaborate on why this is the case feel free