$group and accumulator lab

I am getting the error “$regex has to be a string”. I used this in a $match stage

$match: {
            awards: { $exists: true },
            awards: { $regex: { input: "$awards", regex: '/Won \d{ 1, 2} Oscars/' } },
            "imdb.rating": { $ne: "" }
        }

Please need help.
Thanks!

See https://www.mongodb.com/docs/manual/reference/operator/aggregation/regexMatch/

Specifying, awards twice in the same object will not work. Most JSON library do not keep duplicated field labels.

You can check that in the shell with:

match_data = { awards: { $exists: true },
  awards: { $regex: { input: "$awards", regex: '/Won \d{ 1, 2} Oscars/' } },
  "imdb.rating": { $ne: "" } }

you will get the following object since the first occurrence of awards is overwritten by the second one.

{
  awards: { '$regex': { input: '$awards', regex: '/Won d{ 1, 2} Oscars/' } },
  'imdb.rating': { '$ne': '' }
}

2 Likes

I got it, thanks so much!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.