2016-07-30 5 views
0

mongodb 3.2 용 java 드라이버를 사용하여 $ 조회 컬렉션에서 집계 $ 일치를 수행하는 방법을 알고 싶습니다. 두 식별자에mongodb 3.2 조회시 Java 드라이버 집계 whith 일치

coll_one:{ 
_id : ObjectId("hex_string"), 
foreign_id : ObjectId("hex_string") **the id of coll_two**} 

coll_two:{ 
_id : ObjectId("hex_string"), 
actif : true, 
closed : false } 

룩업은 (& coll_two._id을 coll_one.foreign_id) 잘 작동하는 것 같다 : 여기에 내가 작업하고있는이 컬렉션의 구조입니다. 그러나 coll_two.actif = true에서 일치 항목을 지정하면 빈 결과가 반환됩니다.

이 사용 자바 코드 i'am입니다 : 내가 일치하는 부분을 제거 whene

Bson lookup = new Document("$lookup", 
new Document("from", "coll_two" ) 
.append("localField", "foreign_id") 
.append("foreignField", "_id") 
.append("as", "look_coll")); 

List<Bson> filters = new ArrayList<Bson>(); 
filters.add(lookup); 

//here is the MATCH 
filters.add(match(eq("look_coll.actif",true))); 

DB().getCollection("coll_one").aggregate(filters); 

Evrything가 잘 작동합니다. 나는 전혀 성공하지 못했을 가능성이 너무 많았습니다!

가능한 경우 어떤 본문이 나에게 알려주습니까?

+0

코드는 내 컴퓨터에 예제 데이터로 작업을한다. 귀하의 데이터가 실제로 당신이 기대하는 것임을 확인하십시오. 예를 들어, actif는 부울 ** true ** 문자열 "true"가 아닙니다. 또한 문서와 같이 일치 단계를 정의 할 수 있습니다 :'Bson match = new Document ("$ match", 새 문서 ("look_coll.actif", true)); –

답변

0

Document 예에 $match 요청을 추가 : 여기

Bson match = new Document("$match", 
       new Document("look_coll.actif", true)); 

filters.add(match); 

전체 예입니다

MongoClient mongoClient = new MongoClient("localhost"); 

MongoDatabase db = mongoClient.getDatabase("mydb"); 

Bson lookup = new Document("$lookup", 
     new Document("from", "coll_two") 
       .append("localField", "foreign_id") 
       .append("foreignField", "_id") 
       .append("as", "look_coll")); 

Bson match = new Document("$match", 
     new Document("look_coll.actif", true)); 

List<Bson> filters = new ArrayList<>(); 
filters.add(lookup); 
filters.add(match); 

AggregateIterable<Document> it = db.getCollection("coll_one").aggregate(filters); 

for (Document row : it) { 
    System.out.println(row.toJson()); 
}