I'm currently learning about MongoDB and Prisma in JavaScript. The following code snippet shows a MongoDB query using Prisma's aggregateRaw method. In this case, the cond
is used to perform a case-insensitive string comparison. For instance, if $$property.property_name
is "The Brough", a regex /the br/i
should evaluate to true.
const result = await prisma.owned_properties.aggregateRaw({
pipeline: [
{ $match: { organization_id: { $oid: "6290eb7843e100027b70dd78" } } },
{
$project: {
properties: {
$filter: {
input: "$properties",
as: "property",
cond: {
// $eq: ["$$property.property_name", "The Brough"],
$regexMatch: {
input: "$$property.property_name",
regex: new RegExp("The Brou", "i"),
// regex: /The Brough/i,
},
},
},
},
_id: 0,
},
},
],
})
An issue arises when Prisma encounters an error due to not recognizing the RegExp or /regex/ notation. Surprisingly, this exact query functions correctly from the mongosh command line interface.
Error occurred during query execution: ConnectorError(ConnectorError { user_facing_error: None, kind: RawDatabaseError { code: "unknown", message: "Command failed (Location51105): Failed to optimize pipeline :: caused by :: $regexMatch needs 'regex' to be of type string or regex)" } })
Does anyone have any suggestions on how to resolve this? Essentially, I just need the query to execute a case-insensitive string comparison.