Meteor mongodb数据透视和更优雅的代码

Meteor mongodb data pivot and more elegant code

本文关键字:代码 mongodb 数据 透视 Meteor      更新时间:2023-09-26

我有一个bin,其中包含所有相关的详细信息:http://jsbin.com/ribor/1/edit?js,输出

但为了让SO轻松回答,以下是详细信息。我有以下模型示例:

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
  })

但这给了我一个按Order&时间戳。我正在寻找的输出是一个按唯一位置排序的数组&时间戳。不完全确定如何到达那里,但我一直在尝试不同的地图&减少方法,因为我感觉这是一条路要走,但我被卡住了。非常感谢您的帮助!

BONUS此外,我的方法感觉不合法,因为必须返回订单对象并使用+=进行增量。我觉得可能有更好的方法可以做到这一点。

我只想添加第二个答案来回答@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}}

为什么不只是:

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

如果您真的想要一个数组而不是一个游标,那么在末尾添加.fetch()