MongoDB - How to select only numeric strings / Check whether string is numeric in mongo-shell

StackOverflow https://stackoverflow.com/questions/16618519

  •  29-05-2022
  •  | 
  •  

Pergunta

I have a collection where the fields are string but those strings can have a numeric value inside e.g.:

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

How can I query "111" and "4502" and any other numeric values in string format?

Foi útil?

Solução

You can use a regular expression in your query object to do that:

// 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+$/})
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top