Home > Back-end >  Find the documents which contains a specific value in a array
Find the documents which contains a specific value in a array

Time:06-21

I want to find documents that contain a single - (symbol).

occupationalCategory array consists of single - (symbol) instead of a double on specific employerId.

wrongly inserted with (single - symbol) "occupationalCategory" : [ "15-1132.00 - Software Developers, Applications" ],

its should be : (double -- symbol) "occupationalCategory" : [ "15-1132.00 -- Software Developers, Applications" ]

Please help me to get those documents.

CodePudding user response:

As you mentioned that the string pattern is consistent, you can use regex to match the string pattern.

^\d -\d .\d{2} - [\w\s] , \w $

^ - Start with

\d - Match with digit

- Refer previous character/symbol with at least 1 occurrence

- - - Symbol

\d .\d{2} - Match for decimal pattern

\w - Word character

\s - Spacing character

$ - Match End

Sample Regex 101 & Test Output

db.collection.find({
  "occupationalCategory": {
    $regex: "\\d -\\d .\\d{2} - [\\w\\s] , \\w ",
    $options: "m"
  }
})

Sample Mongo Playground

  • Related