MongoDB正则表达式搜索整数值

时间:2010-05-25 20:02:41

标签: regex mongodb mongodb-query aggregation-framework

我希望在MongoDB中使用正则表达式搜索整数值。这可能吗?

我正在构建一个CRUD类型的接口,允许*用于各种字段的通配符。我试图保持UI与整数字段保持一致。

考虑:

> db.seDemo.insert({ "example" : 1234 });
> db.seDemo.find({ "example" : 1234 });
{ "_id" : ObjectId("4bfc2bfea2004adae015220a"), "example" : 1234 }
> db.seDemo.find({ "example" : /^123.*/ });
> 

如您所见,我插入一个对象,我可以通过值找到它。如果我尝试一个简单的正则表达式,我实际上找不到该对象。

谢谢!

2 个答案:

答案 0 :(得分:40)

如果你想在数字上进行模式匹配,在mongo中进行模式匹配的方法是使用$ where表达式并传入模式匹配。

> db.test.find({ $where: "/^123.*/.test(this.example)" })
{ "_id" : ObjectId("4bfc3187fec861325f34b132"), "example" : 1234 }

答案 1 :(得分:8)

我不是使用$where查询运算符的忠实粉丝,因为它评估查询表达式的方式,如果查询使用用户输入数据,它不使用索引和安全风险。

从MongoDB 4.2开始,您可以使用MongoDB 4.1.9+和$regexMatch|$regexFind|$regexFindAll中提供的$expr来执行此操作。

let regex = /123/;
  • $regexMatch$regexFind

    db.col.find({
        "$expr": {
            "$regexMatch": {
               "input": {"$toString": "$name"}, 
               "regex": /123/ 
            }
        }
    })
    
  • $regexFinAll

    db.col.find({
        "$expr": {
            "$gt": [
                { 
                    "$size": { 
                        "$regexFindAll": { 
                            "input": {"$toString": "$name"}, 
                            "regex": "123" 
                        }
                    }
                }, 
                0
            ]
        }
    })
    

从MongoDB 4.0开始,您可以使用$toString运算符作为$convert运算符的包装器,以 stringify 整数。

db.seDemo.aggregate([ 
    { "$redact": { 
        "$cond": [ 
            { "$gt": [ 
                { "$indexOfCP": [ 
                    { "$toString": "$example" }, 
                    "123" 
                ] }, 
                -1 
            ] }, 
            "$$KEEP", 
            "$$PRUNE" 
        ] 
    }}
])

如果你想要的是从版本3.4开始检索包含特定子字符串的所有文档,你可以使用允许进行$redact逻辑处理的$cond运算符。$indexOfCP

db.seDemo.aggregate([ 
    { "$redact": { 
        "$cond": [ 
            { "$gt": [ 
                { "$indexOfCP": [ 
                    { "$toLower": "$example" }, 
                    "123" 
                ] }, 
                -1 
            ] }, 
            "$$KEEP", 
            "$$PRUNE" 
        ] 
    }}
])

产生:

{ 
    "_id" : ObjectId("579c668c1c52188b56a235b7"), 
    "example" : 1234 
}

{ 
    "_id" : ObjectId("579c66971c52188b56a235b9"), 
    "example" : 12334 
}

在MongoDB 3.4之前,您需要$project您的文档并添加另一个计算字段,该字段是您的数字的字符串值。

$toLower和他的兄弟$toUpper运算符分别将字符串转换为小写和大写,但它们有一些未知的功能,即它们可用于将整数转换为字符串。

$match运算符使用$regex运算符返回与您的模式匹配的所有文档。

db.seDemo.aggregate(
    [ 
        { "$project": { 
            "stringifyExample": { "$toLower": "$example" }, 
            "example": 1 
        }}, 
        { "$match": { "stringifyExample": /^123.*/ } }
    ]
)

产生:

{ 
    "_id" : ObjectId("579c668c1c52188b56a235b7"), 
    "example" : 1234,
    "stringifyExample" : "1234"
}

{ 
    "_id" : ObjectId("579c66971c52188b56a235b9"), 
    "example" : 12334,
    "stringifyExample" : "12334"
}

现在,如果你想要的是检索包含特定子字符串的所有文档,那么更容易和更好的方法是使用$redact运算符在即将发布的MongoDB版本中(截至撰写本文时)允许$cond itional逻辑处理。$indexOfCP

db.seDemo.aggregate([ 
    { "$redact": { 
        "$cond": [ 
            { "$gt": [ 
                { "$indexOfCP": [ 
                    { "$toLower": "$example" }, 
                    "123" 
                ] }, 
                -1 
            ] }, 
            "$$KEEP", 
            "$$PRUNE" 
        ] 
    }}
])