メソッドを以下に更新できます。複数の$and
を提供しようとしています それぞれに基準がある演算子。
ところで、明示的なand
は必要ありません mongodbとしてのingは、暗黙のand
を提供します 基準がコンマで区切られている場合。
public void getFile(Map<String, Object> metaData) throws Exception {
Criteria criteria = new Criteria();
metaData.forEach((k, v) -> criteria.and("metadata." + k).is(v));
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(new Query(criteria));
if (gridFSDBFile == null) {
throw new HttpConflictException();
}
明示的なand
が必要な場合 以下のコードを使用できます
public void getFile(Map<String, Object> metaData) throws Exception {
Criteria criteria = new Criteria();
Criteria[] andExpressions = metaData.entrySet().stream().
map(kv -> Criteria.where("data." + kv.getKey()).is(kv.getValue()))
.toArray(Criteria[]::new);
Query andQuery = new Query();
Criteria andCriteria = new Criteria();
andQuery.addCriteria(andCriteria.andOperator(andExpressions));
GridFSDBFile gridFSDBFile = gridFsOperations.findOne(andQuery);
if (gridFSDBFile == null) {
throw new HttpConflictException();
}