MongoDB - 如何只选择数字字符串/检查字符串是否为mongo-shell中的数字

Jen*_*nha 6 mongodb mongo-shell

我有一个集合,其中字段是字符串,但这些字符串可以有一个数值,例如:

myObject:{
examples:[
         {example:"words",...},
         {example:"more words",...},
         {example:"111",...},
         {example:"4502", ......}
         ...
         ]
...
}

如何以字符串格式查询"111"和"4502"以及任何其他数值?

Joh*_*yHK 16

您可以在查询对象中使用正则表达式来执行此操作:

// Select docs where at least one examples element contains an example value
// that's made up only of digits.
db.test.find({'examples.example': /^\d+$/})
Run Code Online (Sandbox Code Playgroud)