【问题标题】:mongoose query regex to ignore middle character猫鼬查询正则表达式以忽略中间字符
【发布时间】:2020-06-22 04:43:30
【问题描述】:

我正在使用 mongodb,我的数据存储为 day:"3/6/2020" 我想查询与 day:"3/2020" 匹配的字符串 strong> 没有中间值或省略 day:"3/6/2020" 中的 6。 比如

`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" 与 匹配一样“2020 年 3 月 6 日”

【问题讨论】:

    标签: node.js regex mongodb mongoose mongodb-query


    【解决方案1】:

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

    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 的第一阶段。希望这会有所帮助

    【讨论】:

    【解决方案2】:

    这可能会对你有所帮助。我们匹配 3,而不是一个或两个字符宽的数字,然后在 2020 年底。

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

    https://regex101.com/r/jn4zwa/1

    【讨论】:

    • 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);` ` await FruitModel.find({"day": {$regex:`queryDate,$options:"/\d{1,2}"}}, (err, records)=> { `console.log(records);` res.status(200).send({msg:"successs", record:records});`})
    • monthString +'\/'+'\d{1,2}'+'\/'+yearString;可能发生的第一个错误。
    • 你熟悉正则表达式中的转义吗?
    【解决方案3】:
    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});
            }
        });
    }```
    

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2021-06-04
      • 1970-01-01
      • 2017-08-31
      • 2013-05-10
      • 2014-03-04
      • 1970-01-01
      • 2018-05-14
      • 2016-03-30
      相关资源
      最近更新 更多