I am trying to search for users based on their names. My user schema looks like:
user: {
firstName: string,
lastName: string,
...
}
I can run the following query fine:
const userDocs = await UserModel
.find({
$expr: {
$eq: [
{
$concat: [
'$firstName',
' ',
'$lastName',
],
},
'John Doe',
],
},
});
However I am trying to run a query such as this:
const userDocs = await UserModel
.find({
$expr: {
$regex: [
{
$concat: [
'$firstName',
' ',
'$lastName',
],
},
new RegExp(`^${text}`, 'i'),
],
},
});
However MongoDB does not support this.
Is there a way to use $regex with a $concat?