正则表达式筛选无效
直接写字符串有效,但是用变量就是不对
title: new RegExp(`java`, 'i') //可以使用
title: new RegExp(value, 'i') //搜索不出来
但是请求的条件这里是一样的,为什么
title:
{$regexp: {source: "java", flags: "i"}}
$regexp:
{source: "java", flags: "i"}
flags: "i"
hws007 - 我就是我!
给你加多一点,弄个多字段查询!
let value = ''; //模糊查询
let field = "name,model" //多字段模糊查询
let fieldArr = field.split(',');
let where = '';
if (fieldArr.length) {
fieldArr.forEach((item, index) => {
let or = index == 0 ? "(" : " || ";
let bs = index == fieldArr.length - 1 ? ")" : "";
where += or + `${new RegExp(value, 'i')}.test(${item})` + bs;
});
}
db.collection("uni-id-users").where(where).get()
1***@qq.com (作者)
也不行
2023-05-09 16:51