$regularExpression operator in Mongodb not working

Viewed 1696

When I run the query,

db.getCollection('resources').find({
  "owner.firstName": {
    "$regularExpression": {
      "pattern": "^Del",
      "options": "i"
    }
  }
})

it shows error mssg,

Error: error: {
"ok" : 0,
    "errmsg" : "unknown operator: $regularExpression",
    "code" : 2,
    "codeName" : "BadValue"
}

Note: Mongodb version is 4.4.0-rc13,This query is derived from,

private List<Criteria> buildNameSearch(String type,String name){
        String names[] = name.split(" ");
        ArrayList<Criteria> cr = new ArrayList<Criteria>();
        for (String nm : names) {
            Criteria criteria = new Criteria();
            criteria.orOperator(Criteria.where(type+".firstName").regex("^"+nm),
                    Criteria.where(type+".lastName").regex("^"+nm));
            cr.add(criteria);
        }
        return cr;
    }
Criteria.where(type+".firstName").regex("^"+nm)

Please help me with this...

1 Answers
db.getCollection('resources').find({
  "owner.firstName": {
      "$regex": "^Del", //regex and pattern goes here
      "$options": "i". //regex options goes here
    }
  }
})
  1. Use $regex instead $regularExpression - $regex

  2. There is no pattern.

  3. Use $options

Your spring query looks good.

Related