'How to find middle of the string, next to space and dot in MongoDB

 [
    {
        "Name": "Dr.Soma",
        "Email": "[email protected]",
        "MobNo": 111111111
    }, 
   {
        "Name": "Bootha Ganesh",
        "Email": "[email protected]",
        "MobNo": 222222222
    }, 
   {
        "Name": "Steven",
        "Email": "[email protected]",
        "MobNo": 333333333
    },
    {
            "Name": "Dr.Anbarasi",
            "Email": "[email protected]",
            "MobNo": 4444444444
     }
     ]

I try this to using find regex

db.details.find({Name:{$regex:/steven/i}})

output:

   {
      "Name": "Steven",
      "Email": "[email protected]",
      "MobNo": 333333333
    }

How to find data Name dot(.) after Soma & Space after Ganesh
Excepted Output
If I find Name Ganesh,I need

{
            "Name": "Bootha Ganesh",
            "Email": "[email protected]",
            "MobNo": 222222222
  }

If I find Name small s or capital S ,I need

{
        "Name": "Dr.Soma",
        "Email": "[email protected]",
        "MobNo": 111111111
 }

No Need Name Dr.Anbarasi data



Solution 1:[1]

db.collection.find({"Name": {'$regex': /\bsoma[a-zA-Z0-9]*/gi}})

\b assert position at a word boundary: (^\w | \w$ | \W\w|\w\W)
soma-for searching value
[a-zA-Z0-9]-word characters
*-to entire string

Solution 2:[2]

db.collection.find({ Name: { $regex: "Soma" } })

mongoplayground


db.collection.find({ Name: { $regex: ".Soma" } })

mongoplayground


db.collection.find({ Name: { $regex: " Ganesh" } })

mongoplayground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ITHRIS MOHAMED
Solution 2 YuTing