情况是这样的,由于数据库需要查询的字段涉及到正则的特殊字符,例如(、-等。
一开始我这样查,根本查询不到内容:
return await db.collection(event.collection).aggregate()
.match({
equipmentName: new db.RegExp({
regexp: '茂名茂南区华侨新村宿舍I4二级(扩)',
options: 'i',
})
})
.group({
_id: {
_id: '$_id',
department1: '$equipmentName',
department2: '$boxName'
}
})
.end();
后来我想到应该是我未转义特殊字符,于是我这样查,但仍然是查不到内容:
return await db.collection(event.collection).aggregate()
.match({
equipmentName: new db.RegExp({
regexp: '茂名茂南区华侨新村宿舍I4二级\(扩\)',
options: 'i',
})
})
.group({
_id: {
_id: '$_id',
department1: '$equipmentName',
department2: '$boxName'
}
})
.end();
后来我尝试这样,竟然能查询到内容:
return await db.collection(event.collection).aggregate()
.match({
equipmentName: /茂名茂南区华侨新村宿舍I4二级\(扩\)/i
})
.group({
_id: {
_id: '$_id',
department1: '$equipmentName',
department2: '$boxName'
}
})
.end();
是不是证明db.RegExp转义存在BUG?
另外,希望官方简单说明一下关于RegExp查询的转义,官方建议的写法是怎样的?谢谢