Return results where first letter of field is X

Hi all,

Really simple question, but I can’t get my head around it. If I want to return all the results in a collection where a particular field starts with the letter ‘A’, I can write the following:

return People.find({'name' : /^A/});

This works fine. However, How would I make the ‘A’ dynamic? In other words, I need something like this:

return People.find({'name' : /^variableGoesHere/});

The above obviously doesn’t work. Any advice on how to do this?

Have you tried

const variableGoesHere = '^X';
return People.find({'name' : { $regex: variableGoesHere} });

Thanks - that works perfectly!