I am trying to locate a specific row in MongoDB using OR and I need to include a JavaScript variable inside the regex. However, when I do this, the query returns null. By replacing /${oriFilename}/
with my file name like "CB0123456781_20210604165222", the query works as expected.
Can someone please guide me on how to correctly insert the JavaScript variable inside the regex?
let oriFilename = files[file]['name'].replace(".txt", "") //returns CB0123456781_20210604165222
const queryExisted = {
project_id: ObjectId(project._id),
test_station: fields.station,
serial_number: serialNumber,
mac_address: macAddress,
$or: [
{ data_txt_filename: { $regex: /${oriFilename}/, $options: 'i' } },
{ log_txt_filename: { $regex: /${oriFilename}/, $options: 'i' } },
{ comport_txt_filename: { $regex: /${oriFilename}/, $options: 'i' } },
{ telnet_txt_filename: { $regex: /${oriFilename}/, $options: 'i' } },
]
}
let existedData = await db.collection("ate_tests").findOne(queryExisted)