Short answer: No
Criteria
has Querydsl-Like syntax which allows the generation of type-safe queries. Reading from Document
may introduce MongoDB incompatible commands or syntax for the current MongoDB version.
Long answer: Yes
We can implement walkaround with Java reflection to add Document key:value
into Criteria private
fields.
Warning: You assume your Document
has compatible operators with correct syntax.
Add this helper method into your class (souce):
public static Criteria from(Document document) {
Criteria c = new Criteria();
try {
Field _criteria = c.getClass().getDeclaredField("criteria");
_criteria.setAccessible(true);
@SuppressWarnings("unchecked")
LinkedHashMap<String, Object> criteria = (LinkedHashMap<String, Object>) _criteria.get(c);
for (Entry<String, Object> set : document.entrySet()) {
criteria.put(set.getKey(), set.getValue());
}
Field _criteriaChain = c.getClass().getDeclaredField("criteriaChain");
_criteriaChain.setAccessible(true);
@SuppressWarnings("unchecked")
List<Criteria> criteriaChain = (List<Criteria>) _criteriaChain.get(c);
criteriaChain.add(c);
} catch (Exception e) {
// Ignore
}
return c;
}
Now create new instance for Criteria
from Document
:
Criteria newCriteria = from(criteria.getCriteriaObject());
//You have full-compatible Criteria instance
newCriteria.and("foobar").elemMatch(Criteria.where("field").is("value"));