Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将变量与mongo';比如';比较数字时的操作?_Javascript_Node.js_Api_Mongodb Query - Fatal编程技术网

Javascript 如何将变量与mongo';比如';比较数字时的操作?

Javascript 如何将变量与mongo';比如';比较数字时的操作?,javascript,node.js,api,mongodb-query,Javascript,Node.js,Api,Mongodb Query,我在没有使用单独变量search_num的情况下尝试了同样的方法,并使用了search变量本身,但同样的问题仍然存在,这就是我打字的原因。他接受了邮递员的测试 这是我的模式- const lender_details = mongoose.Schema({ lender_id : Number, investment_id: Number, name: String, email: String, mobile_no

我在没有使用单独变量search_num的情况下尝试了同样的方法,并使用了search变量本身,但同样的问题仍然存在,这就是我打字的原因。他接受了邮递员的测试 这是我的模式-

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/概念不起作用,因此我使用了regex,但在尝试了几乎所有操作之后,当我尝试搜索作为数字的投资ID时,此错误仍然存在- 模型“贷方详细信息”的路径“投资id”处的值“/1/”转换为数字失败
感谢您的帮助

Iirc您应该首先使用一些搜索管道将其转换为字符串,然后使用regex搜索它; 让我举一些例子

a.find({
"$expr": {
    "$regexMatch": {
       "input": {"$toString": "$investment_id"}, 
       "regex": search_num_regexp 
    }
})

iirc您可以使用where运算符,但它可能会降低性能查询,我不确定它是否能正常工作。但它会像下面这样

a.find({
  $where: `${search_num_regexp}.test(this.example)`
})

'/1/'
不是数字,为什么要将其强制转换为
number(search)
…?在mongodb中搜索不需要提供正则表达式。只需使用:`wait a.find({“investment_id”:search_num,“email”:search},{u id:false})`Hi,原因是我不想做精确的搜索,想做一些类似于SQL中%m%的事情,希望这能回答您的问题
{$regex:new RegExp(req.body.search)}
。@deceze相同的错误仍然存在:)