Regex flag /g not supported in aggregation on Serverless cluster?

Trying this $match stage on a normal Atlas cluster

{
  "SomeField": { $regex: 't', $options: 'g' }
}

works fine and returns the matched documents.

Trying the same thing on a serverless cluster returns this error:

Command aggregate failed: invalid flag in regex options: g.

Is this a known limitation, or should I open a bug ticket?

Hi @John_Knoop,

Thanks for raising this. Serverless instances run on the latest MongoDB release of MongoDB (Version 5.3 as of the time of this message). As noted in the version 4.4 and 5.0 $regex documentation:

The $regex operator does not support the global search modifier g .

I did some testing with an Atlas cluster with the MongoDB version set to `Latest Release (auto-upgrades) which was version 5.3:

The error is returned when the g option is passed through.

Starting in MongoDB 5.1, invalid $regex options options are no longer ignored as per the noted changes. I presume the MongoDB cluster which did not return the error is at MongoDB version 5.0 or lower however please correct me if I am wrong here.

Regards,
Jason

1 Like

Yes, the other cluster is version 4.x.

Hmm, so the /g flag didn’t work before either, it just wasn’t reported?

Hi @John_Knoop,

Hmm, so the /g flag didn’t work before either, it just wasn’t reported?

Starting at version 4.4 onwards, the $regex operator does not support the global search modifier g as noted in the docs:

It was silently ignored in version 4.4 and 5.0 which is why no error is returned. As noted in my previous comment, starting in MongoDB Version 5.1, invalid $regex options are no longer ignored which resulted in the error you received when running the aggregation example you provided with the /g flag against a Serverless instance (which would have been MongoDB version 5.3 judging by the time of this post but correct me if i’m wrong here).

Regards,
Jason

2 Likes

I see.

Do you happen to know why support for /g was removed?

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