dimanche 27 mars 2022

Mongodb: $regexMatch after converting array $toString not working as expected

I'm using Mongo's $regexMatch operator to find documents where at least part of a field matches the pattern, and this works fine for root-level fields, but the search document has an array with an array field inside. But how do I use it with array fields? I want to return a match if at least one of the array elements matches the pattern.

db.companies.aggregate(
[{$geoNear: {
 near: {
  type: 'Point',
  coordinates: [
   -3.128145200960907,
   -59.99944718666838
  ]
 },
 maxDistance: 10000,
 distanceField: 'dist.calculated',
 spherical: true
}}, {$lookup: {
 from: 'flyers',
 localField: '_id',
 foreignField: 'company',
 as: 'flyers'
}}, {$project: {
 _id: 1,
 socialReason: 1,
 'profile.logo': 1,
 flyers: {
  $filter: {
   input: '$flyers',
   as: 'flyers_ativados',
   cond: {
    $eq: [
     '$$flyers_ativados.status',
     'Ativado'
    ]
   }
  }
 }
}}, {$project: {
 flyers: {
  $filter: {
   input: '$flyers',
   as: 'flyers_ativados',
   cond: {
    $or: [
     {
      $regexMatch: {
       input: '$$flyers_ativados.title',
       regex: 'kuka'
      }
     },
     {
      $regexMatch: {
       input: '$$flyers_ativados.description',
       regex: 'kuka'
      }
     },
     {
      $regexMatch: {
       input: {
        $toString: '$$flyers_ativados.tags'
       },
       regex: 'carne'
      }
     }
    ]
   }
  }
 }
}}])

MongoServerError: PlanExecutor error during aggregation :: caused by :: Unsupported conversion from array to string in $convert with no onError value

very grateful for any suggestion

Aucun commentaire:

Enregistrer un commentaire