我想在MongoDB中搜索一个整数值。这可能吗?

我正在构建一个CRUD类型的接口,该接口允许 *在各个字段上的通配符。我正在尝试使UI在几个整数字段中保持一致。

考虑:

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

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

谢谢!

有帮助吗?

解决方案

如果您想对数字进行模式匹配,那么在Mongo中进行的方式是使用$表达式和通过模式匹配。

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

其他提示

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

从mongodb 4.2开始,您可以使用 $regexMatch|$regexFind|$regexFindAll 在MongoDB 4.1.9+和 $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" 
        ] 
    }}
])

如果您想要的是检索包含特定子字符串的所有文档,从发行版本开始,您可以使用 $redact 操作员允许 $condiTional逻辑处理。$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"
}

现在,如果您想要的是检索包含特定子字符串的所有文档,那么这样做的更轻松,更好的方法是在即将发布的MongoDB(截至撰写本文中)的版本 $redact 操作员允许 $condiTional逻辑处理。$indexOfCP.

db.seDemo.aggregate([ 
    { "$redact": { 
        "$cond": [ 
            { "$gt": [ 
                { "$indexOfCP": [ 
                    { "$toLower": "$example" }, 
                    "123" 
                ] }, 
                -1 
            ] }, 
            "$$KEEP", 
            "$$PRUNE" 
        ] 
    }}
])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top