Lab - Computing Fields $regex vs $split and $size

I tried suggested way with $split + $size and also with $regex.
And $regex seems much slower. I’m curious why?

I believe this is an M121 Aggregation Framework question?

yep, thanks. misclicked.

It depends on your use of $regex but generally it may be less performant than the alternative. Refer to the index use section of the $regex documentation.

Basically, if you’re ever going to use regex, it’s recommended to always use a caret, i.e. a begins with expression /^a/ which efficiently uses the index. This /^a.*/ will incur a slight performance cost, anything else, /a/ or /a.*$/, will be much slower.

In addition, if your regex is placed within a stage where the queried field is a computed field that no longer contains an index, then performance degrades even more.

The only way to know how well your query is performing is by looking at the Explain Plan.