Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Node.js mongoose查询正则表达式以忽略中间字符_Node.js_Regex_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

Node.js mongoose查询正则表达式以忽略中间字符

Node.js mongoose查询正则表达式以忽略中间字符,node.js,regex,mongodb,mongoose,mongodb-query,Node.js,Regex,Mongodb,Mongoose,Mongodb Query,我正在使用mongodb,我的数据存储为天:“3/6/2020”我想查询与天:“3/2020”匹配的字符串,不带中间值或省略天中的6:“3/6/2020”。 比如 `myModel.find({"day": {$regex: "3/2020", $options:" what option to pass here to ignore the ` ` middle value"}});` or any better way `model.find({"name": {$regex: sear

我正在使用mongodb,我的数据存储为天:“3/6/2020”我想查询与天:“3/2020”匹配的字符串,不带中间值或省略天中的6:“3/6/2020”。 比如

`myModel.find({"day": {$regex: "3/2020",  $options:" what option to pass here to ignore the `
` middle value"}});`
or any better way
 `model.find({"name": {$regex: search, $options:" regex options"}}, (err, users)=> {`
        `  res.status(200).send({users});`

“3/2020”将任何记录与32020匹配,就像此“3/2020”与“3/6/2020”匹配一样,这可能会对您有所帮助。我们匹配3,比一个数字宽一到两个字符,然后在2020年底

3\/\d{1,2}\/2020

我想您可以使用聚合框架将字符串日期转换为正确的日期,获取月份和年份,并与之正确匹配。像这样的

model.aggregate([{
    "$addFields": {
        "convertedDate": {
            "$toDate": "$myDate" // mydate is your field name which has string
        }
    }
}, {
    "$addFields": {
        "month": {
            "$month": "$convertedDate"
        },
        "year": {
            "$year": "$convertedDate"
        }
    }
}, {
    "$match": {
        "month": 3, // this is your search criteria
        "year": 2020 // // this is your search criteria
    }
}, {
    "$project": {
        "month": 0, // Do not take temp added fields month and year 
        "year": 0 // Do not take temp added fields month and year 
    }
}])

这看起来像一个大查询,但我想比使用正则表达式进行字符串编译要好得多。如果您的字段以日期格式保存,您还可以删除执行
$toDate
的第一个阶段。希望这对你有所帮助。

model.find({“name”:{$regex:search,$options:“/\d{1,2}\”}
不起作用。你为什么要搜索\d{1,2}?搜索我提供给你的正则表达式:3\/\d{1,2}\/2020对不起,我是从上面复制的。下面是我的代码`let yearString=req.body.year.toString()“`let monthString=req.body.month.toString();`console.log('fire etting…',queryDate);`wait FruitModel.find({“day”):{$regex:`
queryDate$options:“/\d{1,2},(err,records)=>{
`console.log(records);`res status(200).send({msg:“successs”,record:records});`code>})monthString+'\/'+'\d{1,2}'+'\/'+yearString;可能发生的第一个错误之一。您熟悉正则表达式中的转义吗?谢谢,如果您能参考更多示例以符合我的理解,我将不胜感激。阅读-我想这应该会对您有所帮助
Solved. i solved it  splitting my date as in my schema
```
    qDay :{
        type:Number, default: new Date().getDate(Date.now)
    },
    qMonth :{
        type:Number, default: new Date().getMonth(Date.now) + 1
    },
    qYear :{
        type:Number, default: new Date().getFullYear(Date.now)
    }```

my query


```const getByDay = async (req, res)=> {
    console.log(req.body);
    merchantModel.find({$and:[{qMonth : req.body.month}
        ,{qYear: req.body.year},{qDay:req.body.day}]}).then((record)=> {
        if(record.length == 0){
            res.status(404).send({msg:'no record!'});
        }else{
            co`enter code here`nsole.log(record);
            res.status(200).send({record:record});
        }
    });
}```