1

Record in database:

{
    "_id" : ObjectId("5b97b20e7b473d1ee468a972"),
    "name" : "insearch.com wiki",
    "description" : "insearch",
    "createdAt" : ISODate("2018-09-11T12:16:14.181Z"),
    "updatedAt" : ISODate("2018-09-11T12:16:14.181Z"),
    "__v" : 0
}

Search Query

db.getCollection('contents').find({name:{$regex: '.', $options: "i"}})

How can I search the record when I give "." (only dot operator) in search box.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
olagu
  • 597
  • 5
  • 10

1 Answers1

1

The . period character has special meaning in a regex where it matches any character. To disable that behavior and match against the period itself, you need to escape it:

db.getCollection('contents').find({name:{$regex: '\\.', $options: "i"}})

To do this programmatically (as in your case with a search box), see Is there a RegExp.escape function in Javascript?

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471