【问题标题】:How to retrieve and group by Week my meteor mongo data?如何按周检索和分组我的流星 mongo 数据?
【发布时间】:2015-10-29 04:02:12
【问题描述】:

我想检索我的数据并按周分组?

例如:

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

both/collections/hours.js

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

}

client/views/hours.js

// List Hours
Template.showHours.helpers({
     hours: function() {
        return ReactiveMethod.call('groupedByWeek');
      }
 });

服务器/server.js

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

views/hours/hours.html

 {{#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}}

【问题讨论】:

    标签: meteor meteor-autoform


    【解决方案1】:

    首先添加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
        }
    })
    

    现在在你的 Helper 调用中

    return ReactiveMethod.call('groupedByWeek');

    您必须使用响应式方法,否则它将返回 null,因为 Method.call 是异步的

    希望这会有所帮助!

    【讨论】:

    • 我收到此错误消息:无法调用未定义堆栈的方法“聚合”:TypeError:无法在 [object Object].Meteor.methods.groupedByWeek 调用未定义的方法“聚合”(服务器/服务器。 js:306:21) at [object Object].methodMap.(anonymous function) (packages/meteorhacks_kadira/packages/meteorhacks_kadira.js:2554:1) at maybeAuditArgumentChecks (livedata_server.js:1692:12) at livedata_server.js:708 :19 at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1) at livedata_server.js:706:40
    • 还有meteorhacks:aggregate 包?
    • 抱歉忘了提......是的,你必须使用 meteorhacks:aggregate 包
    • 回车Hours.aggregate(query)
    • 它工作正常。你就是那个男人。你能解释一下你的代码吗?这会很有帮助。非常感谢
    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多