I have simple interface with one method:
Criteria toCriteria(String key, String value)
And next I'd like to have next implementation
public class EqExpression implements Expression
{
@Override
public Criteria toCriteria(String key, String value)
{
return Criteria.where(key).eq(Pattern.compile(value));
}
}
}
but there isn't $eq operator. So my questions:
- Why
org.springframework.data.mongodb.core.query.Criteria
doesn't have such operator? Is there a way to implement custom Criteria implementation or is there any workaround?
For me it would be good to have code like
@Override public Criteria toCriteria(String key, String value) { //return new BasicDBObject(key, new BasicDBObject("$eq", value)) converted to Criteria }
In general, my purpose is to implement rest query language and for each operation like gt
, lt
I have specific implementation of Expression
interface.
Request may looks like name=John&age>20
I am building whole query using next code:
List<Criteria> criterias = new ArrayList<Criteria>();
...
while (matcher.find())
{
String key = matcher.group(1);
String operator = matcher.group(2);
String value = matcher.group(3);
// get from map appropriate implementation
criterias.add(expressions.get(operator).toCriteria(key, value));
}
May be you have any suggestions how to implement it more elegant