FIlter entries inside the query

Hi, I’m trying to filter inside the query without any luck.

Currently I have this code:

    const sets = await builder.getAll('open-dataset', {
      options: { noTargeting: true },
      omit: 'data.blocks',
      limit: 100,
      query: {
        data: {
          date: { $gte: newMinYear, $lte: newMaxYear },
          title: { $regex: search, $options: 'i' },
          annotation: { $regex: annotationArray && annotationArray.join(' '), $options: 'i' },
        }
      }
    });

The date and title filter work fine, but the annotation doesn’t.
The annotation field consists in a string of words separated by commas, like Video, Image, GPS or whatever combination with video, image, GPS and lidar.

The annotation array is the filter I want to match, it consists of an array of strings of whatever combination of the same that above (video, image, GPS and lidar).

My objective is to fetch all the entries that contain one or more of the items of the array. So, for example, if the annotation array is [‘Video’, ‘Image’, ‘Lidar’], I want to fetch all the entries that have at least one of them in the annotation field.

Currently, my code isn’t working, because with that $regex I’m searching for those entries that match the entire array… following the example above, those whose annotation field consists in ‘Video, Image, Lidar’, and not the ones that have for example only ‘Video’.

I was wondering if I could do some workaround like this one I found:

db.collection.aggregate([
  {
    "$addFields": {
      "split": {
        "$split": [
          "$diversity",
          ", "
        ]
      }
    }
  },
  {
    "$match": {
      $expr: {
        $gt: [
          {
            $size: {
              "$setIntersection": [
                "$split",
                [
                  "video",
                  "gps"
                ]
              ]
            }
          },
          0
        ]
      }
    }
  },
  {
    "$project": {
      split: false
    }
  }
])

But I’m struggling with it.

Thanks!

Hi @emiliano , you should be able to do this by changing
annotationArray.join(' ')
to
annotationArray.join('|')

1 Like