1

I have a list of events. I would like to search both the title and description fields for a certain phrase and return events in a given radius matching the query.

I am currently using the $or operator, which seems to work. However, I am wondering if there is a better way of doing this? I've created a compound text index that uses both fields; I'm just not sure if it actually gets used.

let queryText = /someString/;

return db.collection('events').aggregate([
  {
    $geoNear: {
      near: [lng, lat],
      distanceField: 'distance',
      maxDistance: radius,
      query: {
        $or: [
            {title: queryText},
            {description: queryText}
        ]
      },
      spherical: true
    }
  }
]).toArray();
Matt
  • 2,953
  • 3
  • 27
  • 46

1 Answers1

0

You can include your query in another argument (remember that $geoNear always must be on top) like this

let queryText = /someString/;
return db.collection('events').aggregate([
    {
        $geoNear: {
            near: [lng, lat],
            distanceField: 'distance',
            maxDistance: radius,
            spherical: true
        },
    {
        $match: {
            $or: [
                {title: queryText},
                {description: queryText}
            ]
        }
    }
]).toArray();

All of the time I use this way, I have never try your way before.