Consider this aggregate query as a sample, and I want to fetch the results based on the code which contains the first 5 digits of the ProductSerial. So I’m using the starts with regex option to pull the data. How to frame the regex string from previous stage values.
You cannot mix aggregation and match (find) expressions, @Pavel_Duchovny - if you are inside $expr then you can only use aggregation syntax, and Field:{$regex: ... is a match expression.
So if you have a previous stage which figures out the “starts with” substring, you can use that inside $expr after assigning it to a variable like you are doing.