2014-04-26 83 views
1

我與這裏的所有相關細節斌:http://jsbin.com/ribor/1/edit?js,output流星MongoDB的數據支點和更優雅的代碼

但這麼回答緩解這裏的細節。我有這些模型的例子:

Orders = { 
    _id: "T487wBz7Pvzjo2QHh", 
    beverages: [ 
    { 
     _id: "8ea26bb103efae385f80f881", 
     delivered: false, 
     name: "Wine", 
     units: "55" 
    } 
    ], 
    location: "yTKWLFwSDvp3DyCkx", 
    locationName: "Ladies Garden", 
    locationNumber: "101", 
    timestamp: 1398393004864, 
    user_id: "W3Fdq36Ts2TdWxCRP", 
    username: "jgeezy" 
}; 

Locations = { 
    _id: "yTKWLFwSDvp3DyCkx", 
    beverages: [ 
    { 
     _id: "e5552a68266ed76895b8228a", 
     fillTo: "10", 
     name: "Wine Coolers", 
     orderWhen: "10", 
     startUnits: "10" 
    } 
    ], 
    name: "Teen Cabana", 
    number: "103", 
    organization: "Super Happy Teens!", 
    vendor: false 
}; 

我需要呈現在每個位置的表將顯示每個位置的未交付訂單飲料的總#某些行。我有這個功能已經寫:

Template.dashboardOrders.undeliveredOrders = -> 
    undeliveredOrders = Orders.find({'beverages.delivered': false}, { 
    transform: (order) -> 
     order.unfilledOrders = 0 
     _.each order.beverages, (bev) -> 
     if not bev.delivered 
      order.unfilledOrders += 1 
     order 
    }) 

但是,讓我通過,&時間戳排序的數組。我正在查找的輸出是按照唯一位置&時間戳排序的數組。不完全確定如何到達那裏,但我一直在嘗試不同的地圖,因爲我覺得這是要走的路,但我被卡住了。任何幫助深表感謝!

BONUS另外,由於必須返回訂單對象並使用+ =來遞增,我的方法感覺不夠好看。我覺得可能有更好的方法來做到這一點。

+0

這是比較容易使用下劃線方法'groupBy'或'groupBy',這取決於你想要什麼,但我不太明白的問題。您希望按地點(即「countBy」)還是按位置和時間戳(即「groupBy」)排序的未送達飲料訂單總數?他們顯然將是兩件不同的事情。 – richsilv

回答

1

我只是想添加第二個答案來回應@jasongonzales的第二個問題。我的第一個回答,只是查詢,返回了所有未送達的訂單,並按位置和時間戳排序。根據評論,還需要以下內容:

我想包括每個位置未送達的所有訂單,我需要計算每個位置的未送達訂單數。

有很多方法可以做到這一點。我認爲,最簡單的就是不要試圖人羣這一切到查詢,但查詢返回的數據,而工作:

Template.dashboardOrders.undeliveredOrdersByLocation = -> 
    orders = Orders.find 
     beverages.delivered: no 
    , 
     sort: 
     location: 1 
     timestamp: 1 

    # Create an object, locations, to hold the undelivered orders by location; 
    # the object keys are the location _id's, and the values are the mongo docs: 
    locations = {} 

    # Loop through all the undelivered orders and add each to the locations object: 
    orders.forEach (doc) -> 
    unless locations[doc.location]? 
     locations[doc.location] = 
     undeliveredOrders: [] 
     undeliveredOrdersCount: 0 
     location: doc.location 
     locationName: doc.locationName 
     locationNumber: doc.locationNumber 
     # etc., whatever else your template needs 

    locations[doc.location].undeliveredOrders.push doc 
    locations[doc.location].undeliveredOrdersCount++ 

    # Convert the locations object into an array for use in your template: 
    return _.values(locations) # Don't need to preserve the keys 

你沒有給你的模板的一個例子,但你可以使用locations陣列像這樣:

{{#each undeliveredOrdersByLocation}} 
    There are {{undeliveredOrdersCount}} undelivered orders for {{locationName}}: 
    {{#each undeliveredOrders}} 
     An order submitted by {{username}} on {{timestamp}} for: 
     <ul> 
      {{#each beverages}} 
      <li>{{units}} units of {{name}} need to be delivered.</li> 
      {{/each}} 
     </ul> 
    {{/each}} 
{{/each}} 
+0

Noooooooooooice!綠色複選標記爲你我的兄弟。 – jasongonzales

+0

謝謝。你可以通過創建更多的助手來檢索.undeliveredOrders數組的長度等來避免向對象添加額外的屬性等。它不會真的影響性能,它只是你喜歡如何組織你的代碼。 –

0

爲什麼不乾脆:

Template.dashboardOrders.undeliveredOrders = -> 
    Orders.find 
     beverages.delivered: no 
    , 
     sort: 
     location: 1 
     timestamp: 1 

如果你真的想要一個數組而不是一個遊標,添加.fetch()到最後。

+0

感謝您提供更優雅的查詢,但它仍然只讓我成爲那裏的一部分。我想包括每個地點未送達​​的所有訂單,並且我需要對每個地點的未送達訂單進行計數。 – jasongonzales