2014-04-26 80 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