-1

find records from mongo using spring mongo template where empId (integer) starts with 1100 only and not with 2200 tried regex but it's not returning results when I tried in compass nor from spring project.

Query query = new Query()
    .addCriteria(Criteria.where("empId").regex("^1100"))
    .addCriteria((CriteriaDefinition) Sort.by(Sort.Direction.DESC, "empId"));

{empId:{$regex: /^1100/ }}       //no records found
{"$regexMatch": {"input": {"$toString": empId}, $regex: "^11" }}  // all records returned

NOTE:
if I try on EmployeeName(String) it works. 
    {name:{$regex: /^Wa/ }}


SOLUTION in MONGOCOMPASS: { $where: "/^11.*/.test(this.empId)" }
Learner
  • 99
  • 3
  • 14

0 Answers0