【发布时间】:2021-02-05 10:55:04
【问题描述】:
我尝试了相同的方法,但没有使用单独的变量 search_num 并使用搜索变量本身,但同样的问题仍然存在,这就是我进行类型转换的原因。用邮递员测试过 这是我的架构 -
const lender_details = mongoose.Schema({
lender_id : Number,
investment_id: Number,
name: String,
email: String,
mobile_no : Number,
reg_date: Number,
live_status: String,
lender_details: String,
})
我想做的只是有一个通用的搜索框,通过投资 ID、电话和电子邮件进行搜索。
router.post('/', async (req,res,next) => {
search = req.body.search;
search_num = Number(search);
console.log({$regex : search_num});
await a.find({
"investment_id" :($regex : search_num),
"email" : { $regex : search } }
,{ _id : false })
/m/ 概念在使用变量时不起作用,因此我使用了正则表达式,但在尝试了几乎所有方法后,当我尝试搜索数字投资 ID 时,此错误仍然存在 - 模型“lender_details”的路径“investment_id”中值“/1/”的数字转换失败 任何和所有的帮助表示赞赏!
【问题讨论】:
-
'/1/'不是数字,你为什么要把它转换成Number(search)...? -
在 mongodb 中搜索不需要提供正则表达式。只需使用:` await a.find({ "investment_id" :search_num, "email" : search } ,{ _id : false })`
-
嗨,原因是我不想做精确搜索,想在 SQL 中做类似于 LIKE %m% 的事情,希望这能回答你的问题
-
{$regex: new RegExp(req.body.search)}…? -
@deceze 同样的错误仍然存在:)
标签: javascript node.js api mongodb-query