Javascript 对集合中的条目进行分类

Javascript 对集合中的条目进行分类,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,在构建我的第一个Meteor应用程序时,我发现了一些我甚至不知道如何处理的东西。 我有一个集合中的条目列表,我想按日期对它们进行分类(而不是按日期排序)。每个条目都有一个日期,我想建立一个类似时间线的东西。以下是其中一个条目的外观: { "_id": "xHmfEc5gMmGzDZgD2", "title": "sth", "labels": [ "pv4LnWNheyYRJqG7D" ], "images": [ "zitE3FwmesHsNCxGG",

在构建我的第一个Meteor应用程序时,我发现了一些我甚至不知道如何处理的东西。 我有一个集合中的条目列表,我想按日期对它们进行分类(而不是按日期排序)。每个条目都有一个日期,我想建立一个类似时间线的东西。以下是其中一个条目的外观:

{
  "_id": "xHmfEc5gMmGzDZgD2",
  "title": "sth",
  "labels": [
    "pv4LnWNheyYRJqG7D"
  ],
  "images": [
    "zitE3FwmesHsNCxGG",
    "kYQTJYApDxPr4sMdW",
    "4enopyawfqBXRDvuh"
  ],
  "author": "TMviRL8otm3ZsddSt",
  "createdAt": "2016-01-31T21:05:55.401Z",
  "date": "2016-01-31T21:05:55.401Z",
  "description": "desc"
}
最后,我希望实现一些类似于时间线的东西,我希望条目属于某一天。以下是按天分割的时间轴上的情况,每个条目都属于一天:

日期-2016年2月11日

条目-xHmfEc5gMmGzDZgD2

条目-ZHMFEC5GMGZGZG8Z

条目-UAFEC5GMMGZGD25

日期-2016年2月12日

条目-xHmfEc5gMmGzDZgD2

条目-ZHMFEC5GMGZGZG8Z

日期-2016年2月17日

条目-xHmfEc5gMmGzDZgD2

条目-ZHMFEC5GMGZGZG8Z

条目-xHmfEc5gMmGzDZgD2

条目-ZHMFEC5GMGZGZG8Z

但我想从这些条目中提取天数,而不是为日历创建单独的集合。如果可能的话

实现这一目标的好方法是什么


目前我能想象的唯一方法是将条目中所有不同的日期存储到一个单独的集合中,然后,每天都应该查询条目集合以获得适合的日期。但我认为这是一个非常糟糕的方法,因为每天我都会有一个DB查询……

您可能想使用来实现这一点,但是因为meteor中还不支持聚合,所以您需要安装聚合框架包-它没有什么特别之处,只是为您总结了一些Mongo方法

只要meteor add,你就应该开始做生意了。这将为Meteor添加适当的聚合支持

现在您需要这个管道来实现所需的结果。在mongo shell中,运行以下操作:

var pipeline = [
    {
        "$project": {
            "yearMonthDay": { "$dateToString": { "format": "%Y-%m-%d", "date": "$date" } },         
        }
    },
    {
        "$group": {
            "_id": "$yearMonthDay",
            "entries": { "$push": "$_id" }
        }
    }
];

db.entry.aggregate(pipeline);
在Meteor中,您可以将这些结果发布到客户端的Entries集合:

Meteor.publish('getDayCategorisedEntries', function (opts) {

    var initializing = 1;

    function run(action) {

        // Define the aggregation pipeline ( aggregate(pipeline) )
        var pipeline = [
            {
                "$project": {
                    "yearMonthDay": { "$dateToString": { "format": "%Y-%m-%d", "date": "$date" } },         
                }
            },
            {
                "$group": {
                    "_id": "$yearMonthDay",
                    "entries": { "$push": "$_id" }
                }
            }
        ];

        Entries.aggregate(pipeline).forEach(function(e){
            this[action]('day-entries', e._id, e)
            this.ready()
        });
    };

    // Run the aggregation initially to add some data to your aggregation collection
    run('added');

    // Track any changes on the collection we are going to use for aggregation
    var handle = Entries.find({}).observeChanges({
        added(id) {
            // observeChanges only returns after the initial `added` callbacks
            // have run. Until then, we don't want to send a lot of
            // `self.changed()` messages - hence tracking the
            // `initializing` state.
            if (initializing && initializing--) run('changed');
        },
        removed(id) {
            run('changed');
        },
        changed(id) {
            run('changed');
        },
        error(err) {
            throw new Meteor.Error('Uh oh! something went wrong!', err.message)
        }
    });

    // Stop observing the cursor when client unsubs.
    // Stopping a subscription automatically takes
    // care of sending the client any removed messages.
    this.onStop(function () {
        handle.stop();
    });
});

上述内容会观察到更改,并在必要时重新运行聚合。

日期字段是实际字符串还是日期时间对象?感谢您的回答和链接。“聚合”对我来说是全新的东西,但它似乎正是我所需要的。@Skkyp不用担心,至少这会让你加快聚合框架的速度,它可以简化一些复杂的聚合操作。