2014-03-26 6 views
0

나는이 다음과 같습니다 일부 데이터 (하지 실제 데이터).

{ 
     _id:'cust04', 
     name:'Diarmuid Rellis', 
     address:'Elysium, Passage East', 
     county:'Waterford', 
     phone:'051-345786', 
     email:'[email protected]', 
     quotations:[ 
     { 
      _id:'quot03', 
      supplier_ref:'A2006', 
      date_received: new Date('2013-05-12T00:00:00'), 
      date_returned: new Date('2013-05-15T00:00:00'), 
      supplier_price:35000.00, 
      customer_price:35000.00, 
      orders:[ 
       { 
        _id:'ord03', 
        order_date: new Date('2013-05-20T00:00:00'), 
        del_date: new Date('2013-08-12T00:00:00'), 
        del_address:'Elysium, Passage East, Co. Waterford', 
        status:'BALPAID' 
       } 
      ] 
     }, 
     { 
      _id:'quot04', 
      supplier_ref:'A2007', 
      date_received: new Date('2013-08-10T00:00:00'), 
      date_returned: new Date('2013-08-12T00:00:00'), 
      supplier_price:29600.00, 
      customer_price:29600.00, 
      orders:[ 
       { 
        _id:'ord04', 
        order_date: new Date('2014-03-20T00:00:00'), 
        del_date: new Date('2014-05-12T00:00:00'), 
        del_address:'Elysium, Passage East, Co. Waterford', 
        status:'INPROD' 
       } 
      ] 
     } 
     ] 
    } 

나는 견적 및 주문 배열을 긴장을 풀고, 모든 주문의 투영을 얻기 위해 노력하고 있어요 생산에는 고객 이름, supplier_ref 및 각 주문 날짜가 포함됩니다.

여기 내 쿼리입니다 :

db.customers.aggregate([ 
    { $unwind: "$quotations" }, 
    { $unwind: "$quotations.orders" }, 
    { $match: { 'quotations.orders.status': 'INPROD' } }, 
    { 
    $project: { 
     name: 1, 
      supplier_ref: "$quotations.supplier_ref", 
      order_id: "$quotations.orders._id", 
      order_date: "$quotations.orders.order_date" 
      }  
    }, 
    { 
     $group: { 
      _id: "$order_id" 
     } 

    } 
], function (err, results) { 
console.log(results); 
}) 

쿼리가 성공적으로 실행되지만 그냥 주문 IDS, 필요한 다른 분야의되지 않은 있습니다. 내가 뭘 놓치고 있니?

편집

나는 같은 결과를 기대하고있다 :

"result": [ 
    { 
    "_id" : "orderid01", 
    "name" : "Joe Bloggs", 
    "supplier_ref" : "A1234", 
    "date_ordered" : "2012-04-14" 
    }, 
    { 
    "_id" : "orderid02", 
    "name" : "Joe Bloggs", 
    "supplier_ref" : "A1235", 
    "date_ordered" : "2012-04-16" 
    } 
] 

그래서 같이 내 '그룹'기능에 추가 필드를 추가 할 때 :

$group: { 
     _id: "$order_id", 
     supplier_ref: "$supplier_ref" 

    } 

I 오류 : "그룹 집계 필드 'supplier_ref'는 오브젝트 내부의 표현식으로 정의되어야합니다." 결과 객체와 어떤 식 으로든 연관시켜야합니까?

+2

** 정확하게 ** 답장을 받으셨습니까 (질문에 대한 수정 가능). 그러나이 레벨에서 'order_id'를 그룹화 할 때, 그 밖의 많은 것을 반환 할 수는 없습니다 **. 동일한 내용이 다른 데이터 저장소에도 적용됩니다. –

+0

추가 필드를보고 싶다면 Neil이 언급 한대로 $ group 섹션에 해당 필드를 추가하십시오. – anvarik

+0

내 문제를 더욱 명확하게하기 위해 편집되었습니다. 입력 해 주셔서 감사합니다 –

답변

1

그룹 기능을 모두 제거하면 원하는 결과를 얻을 수있었습니다.