Meteor mongodb数据透视和更优雅的代码
Meteor mongodb data pivot and more elegant code
我有一个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()
。
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何使用Doctrine在MongoDB中存储Javascript代码
- 如何在node.js和MongoDb中混合javascript中的同步和异步代码
- MongoDB使用排序和切片更新嵌套数组,不会对代码进行排序
- 使用 co 和来自同步代码的承诺(mongoDB 示例)
- node-js-mongodb回调不起作用-下面的代码在我的笔记本电脑上起作用,但在另一台电脑上不起作用
- 从sails.js/mongodb中提取公共代码
- Node.JS:MongoDB更新回调返回结果,返回函数代码
- nodejs/mongodb代码中的异步问题
- MongoDB:我如何重新格式化这段代码,以包括名称字段上的排序
- 如何在Java代码中使用JavaScript表达式进行MongoDB查询,就像在MongoShell中一样
- Meteor mongodb数据透视和更优雅的代码
- 如何将.js文件中的JavsScript代码作为存储过程存储在MongoDB中
- node . js,MongoDB代码澄清