RegExp在MongoDB的prisma aggregateRaw中不起作用



我是javascript中mongo和prisma的新手。这是一个使用prisma的aggregateRaw方法的mongo查询。cond应该进行不区分大小写的字符串比较。例如,如果CCD_;The Brough";,正则表达式CCD_ 3应该为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,
},
},
],
})

Prisma抛出一个它不喜欢RegExp或/regex/表示法的错误。不过,这个查询在mongosh命令行中也可以使用。

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)" } })

有什么办法让它发挥作用吗?基本上只是希望查询进行不区分大小写的字符串比较。

prisma这样的ORM有很多层,可能会导致某些问题。像这样的问题,我个人没有发现任何报告的问题,但我没有看得太深。

无论如何,我建议使用以下解决方法,在执行regex匹配之前,只需使用$toLower将属性名称小写,如下所示:

{
$regexMatch: {
input: {$toLower: "$$property.property_name"},
regex: 'the brough',
},
}

Mongo游乐场

最新更新