我想检索我的数据并按周分组?
例如:
12/09 - Data1
12/09 - Data2
13/09 - Data3
26/09 - Data4
并希望他们这样:
from 12/09 to 19/09
1. Data1
2. Data2
3. Data3
from 20/09 to 27/09
1. Data4
Hours = new Mongo.Collection("hours");
Hours.attachSchema(new SimpleSchema({
createdBy: {
type: String,
autoValue: function() {
return Meteor.userId()
}
},
//这里是我想要在每周小组中检索的日期
date: {
type: Date,
label: "Date",
min: new Date("2014-01-01T00:00:00.000Z"),
autoform: {
value: new Date("2014-10-18T00:00:00.000Z")
}
}
}));
}
// List Hours
Template.showHours.helpers({
hours: function() {
return ReactiveMethod.call('groupedByWeek');
}
});
if (Meteor.isServer) {
Meteor.methods({
groupedByWeek: function() {
var query = [{
$group: {
_id: {
$week: "$date"
},
Data: {
$push: "$$ROOT"
}
}
}]; // This Query will return data grouped by week number. date is the field with date
return Hours.Data.aggregate(query); // Use your Collection here
}
});
{{#each hours}}
<tr>
<td valign="middle">{{formatDateHours date}}</td>
<td valign="middle">{{workinghour}}h</td>
<td valign="middle">{{class}}</td>
<td valign="middle">{{project}}</td>
<td valign="middle">{{summary}}</td>
<td valign="middle" align="right">
<div class="table-icon-buttons">
<i class="fa fa-pencil-square-o edit"></i>
<i class="fa fa-times delete"></i>
</div>
</td>
</tr>
{{/each}}
答案 0 :(得分:0)
首先添加meteorhacks:aggregate
和simple:reactive-method
到你的项目。
Meteor仅在服务器端允许聚合功能。
所以你必须按照
编写一个流星方法Meteor.methods({
groupedByWeek: function () {
var query = [
{$group: {_id: { $week: "$date" }, Data: {$push: "$$ROOT"}}}
]; // This Query will return data grouped by week number. date is the field with date
return Collections.Data.aggregate(query); // Use your Collection here
}
})
现在在帮手电话中
return ReactiveMethod.call('groupedByWeek');
你必须使用反应方法,否则它将返回null
,因为Method.call
是异步的
希望这有助于!!