2017-05-22 1 views
0

집계 쿼리를 사용하여 일치하는 문서를 임시 컬렉션에로드하려고합니다. 사실, 모든 일치하는 문서를 MongoDB의 임시 컬렉션에로드 할 수 있지만 java 프로그램에서 for 루프에 Null 포인터 예외가 발생합니다.

나는 완전히 여기에 갇혀 있습니다. 이 시나리오에서 널 포인터 예외의 이유를 알 수 있습니까? 그리고 누구나 같은 제안에 대해 제발 ...

Document query = {"$or":[{"roll":1,"joiningDate":{"$gte":ISODate("2017-04-11T00:00:00Z")}},{"roll":2,"joiningDate":{"$gte": ISODate("2017-03-17T00:00:00Z")}}]}; 

      Document match = new Document("$match",new Document("$or",query)); 

      Document out =new Document("$out","TempCol"); 

      System.out.println("Before Aggregation"); 

      AggregateIterable<Document> resultAgg = collection.aggregate(Arrays.asList(match,out)); 

      System.out.println("After aggregation"); 

      for (Document doc : resultAgg){ 


        System.out.println("The result of aggregation match:-"); 

      } 

      System.out.println("Completed"); 

답변

0

나는 일반적으로 하나의 변수로 구조화 된 파이프 라인을 유지하는 것을 선호합니다.

그러나 일반적인 생각이 여기에 당신이 [] 볼 위치를 {}을 볼 DocumentArrays.asList을 사용할 수 있습니다 : (나 org.joda.time.DateTime 여기를 좋아하는 시공 방법이 무엇이든 함께 Date 객체를 생성 할 때

또한
List<Document> pipeline = Arrays.<Document>asList(
    new Document("$match", 
    new Document("$or", Arrays.<Document>asList(
     new Document("roll", 1) 
     .append("joiningDate", new Document(
      "$gte", new DateTime(2017,04,11,0,0,0, DateTimeZone.UTC).toDate() 
     )), 
     new Document("controlId", 2) 
     .append("joiningDate", new Document(
      "$gte", new DateTime(2017,03,17,0,0,0, DateTimeZone.UTC).toDate() 
     )) 
    )) 
), 
    new Document("$out","TempCol") 
); 

AggregateIterable<Document> resultAgg = controlIssueCollection.aggregate(pipeline); 

확인을) 당신이 UTC로 시간을 가지고 작업한다는 것을 의미합니다. 그리고 쉘에 표시된대로 MongoDB에 저장된 값과 비교한다면 UTC를 의미합니다.

관련 문제