를 사용할 때 다음과 같은 형식의 문서와 모음이 제 1 서브 - 문서 이상을 투사하는 방법 :
{
"_id" : { "$oid" : "67bg............"},
"ID" : "xxxxxxxx",
"senses" : [
{
"word" : "hello",
"lang" : "EN",
"source" : "EN_DICTIONARY"
},
{
"word" : "coche",
"lang" : "ES",
"source" : "ES_DICTIONARY"
},
{
"word" : "bye",
"lang" : "EN",
"source" : "EN_DICTIONARY"
}
]
}
내가 lang=X
과 함께 적어도 하나의 의미가 일치하는 모든 문서를 찾으려면를 source=Y
과 일치하는 senses
만 일치하는 문서를 반환하십시오. lang=X
및 source=Y
DBObject sensesQuery = new BasicDBObject();
sensesQuery.put("lang", "EN");
sensesQuery.put("source", "EN_DICTIONARY");
DBObject matchQuery = new BasicDBObject("$elemMatch",sensesQuery);
DBObject fields = new BasicDBOject();
fields.put("senses",matchQuery);
DBObject projection = new BasicDBObject();
projection.put("ID",1)
projection.put("senses",matchQuery);
DBCursor cursor = collection.find(fields,projection)
while(cursor.hasNext()) {
...
}
내 쿼리가 투사에 일치하는 문서를 작동하지만,하지 :
나는이 시도. 내 쿼리를 실행하는 경우 예를 들어 위의 문서를 촬영,이 결과를 얻을 :{
"_id" : { "$oid" : "67bg............"},
"ID" : "xxxxxxxx",
"senses" : [
{
"word" : "hello",
"lang" : "EN",
"source" : "EN_DICTIONARY"
}
]
}
을하지만 난이 원하는 : 나는 통합에 대해 읽어하지만 난 그것을 사용하는 방법을 이해하지 못했다
{
"_id" : { "$oid" : "67bg............"},
"ID" : "xxxxxxxx",
"senses" : [
{
"word" : "hello",
"lang" : "EN",
"source" : "EN_DICTIONARY"
},
{
"word" : "bye",
"lang" : "EN",
"source" : "EN_DICTIONARY"
}
]
}
MongoDB Java 드라이버에서.
감사
AggregationOutput에는 .into(); 메소드가 없습니다. –
v3.5에서 그렇습니다. :) 그 이유는 이것이 목록으로 출력하기위한 구문 론적 설탕이기 때문에 부차적 인 문제입니다. 나는 AggregateIterable을 반환하는 대답을 업데이트했다. 그 대답의 요점은, (1) 투영법에서 elemMatch가 왜 첫 번째 엔트리를 반환하는지, (2) 집계 프레임 워크를 통해 동일한 필터를 수행하는 방법, (3) MongoDB Java 드라이버. – glytching
좋아요, 이제는 ($ eq : [ "$$ sense.source", 'EN_DICTIONARY'] "를 filterExpression에 추가하는 첫 번째 조건 ("$$ sense.lang ","EN ")? –