使用大数据集中的旧/未发布数据进行刷新

时间:2013-09-14 20:37:13

标签: javascript meteor

我有一个非常大的数据集,我只发布/订阅它的一部分。 (最后100个对象) 但是,我还必须返回并访问此数据集的其他片段,但我无法重新发布其他同名数据集,并且我不希望所有客户端同步“非实时”选择。 / p>

建议的方法是什么?

谢谢,

4 个答案:

答案 0 :(得分:1)

您的发布渠道的名称不必与您要发布的集合名称相对应。您可以从一个集合中发布多个集合:

Meteor.publish('currentArticles', function() {
  return Articles.find({}, {limit: 100});
});

Meteor.publish('allArticles', function(chunk) {
  return Articles.find({}, {limit: 100, skip: 100*chunk});
});

答案 1 :(得分:0)

休伯特的例子是让客户决定要叫什么。如果没关系,我没有什么可补充的。但是,如果你需要比这更封闭,你需要建立一个带有userId列的新集合来公开,

return SlicedArticles.find({_id: Meteor.userId()});

或现有文章对象上的数组,以包含有效的用户ID。

答案 2 :(得分:0)

以下是我实现时间片显示的方法。而不是替换模板生成的表格&订阅的数据集,我创建了一个新表。它有点手动,但现在是。

在服务器上,我只公开一个名为gethistory的方法,它接受切片的上限和下限,fetch()新数据集,如果切片周期小于30分钟则返回获取的数据,否则返回假:

Meteor.methods({
    gethistory: function(utcfrom, utcto) {
        // If interval is lower than 30 minutes
        if ((utcto - utcfrom) < 1800 * 1000) {
                query = Mon.find({
                    "$and": [{
                        "utctime": {
                            "$gt": (new Date(utcfrom))
                            }   
                    }, {
                        "utctime": {
                            "$lt": (new Date(utcto))
                            }   
                    }]  
                    }, {
                    sort: {
                        utctime: 1
                    },  
                    limit: 500 
                })  
                res = query.fetch()
                return res 
        } else {
            return false
        }   
    }   
})

当我需要显示新的数据片段时,我使用客户端的以下代码(e.min&amp; e.max包含片段边界)。在我的代码中,这被称为显示一整年的高图的afterSetExtremes(e)部分,(如果您对高阶图部分感兴趣,请查看http://www.highcharts.com/stock/demo/lazy-loading

Meteor.call('gethistory', e.min, e.max, function(error, data) {
    Session.set('histdata', data)
});

我有一个模板:     Template.hist.historylines = function(){        返回Session.get('histdata')     }

还有一个HTML部分:

<body>
{{> hist}}
</body>

<template name="hist">
  <div>
   {{#if historylines}}
  <table>
    {{#each historylines}}
      {{> line}}
    {{/each}}
   {{else}}
     <p>You must select a period smaller than 30 minutes to get the details.</p>
   {{/if}}
  </table>
  </div>
</template>
<template name="line">
  <tr>
    <td>{{val1}}</td>
    <td>{{val2}}</td>
  </tr>
</template>

答案 3 :(得分:0)

您可以(并且应该)使用发布而不是Meteor.method执行此操作,尤其是在您需要实时更新的情况下。

您可以在服务器上将一个集合发布到客户端上的不同集合,一个是“实时”,另一个不是(可能会节省一些资源,如观察者)。

服务器代码:

Articles = new Meteor.Collection("articles");

// Static articles publication with no observer.
// More efficient than just doing  return Articles.find(); 
Meteor.publish("staticPub", function() {
    var sub = this;
    // Put your arguments in this find() for the "non-live" selection
    Articles.find().forEach(function(article) {
        sub.added("staticArticles", article._id, article);                  
    });

    sub.ready();    
});

// Live latest articles publication
Meteor.publish("latestPub", function() {
    // Only get things at most 30 minutes old
    var cutoff = +new Date - 30 * 60 * 1000;
    return Articles.find({ 
        utctime: {$geq: cutoff} 
    }, {
        limit: 500
    });
});

客户代码:

StaticArticles = new Meteor.Collection("staticArticles");
Articles = new Meteor.Collection("articles");

Meteor.subscribe("staticPub");
Meteor.subscribe("latestPub");

也可以将最新的文章推送到客户端上具有不同名称的集合,尽管这会使这里的代码更加冗长。有关您可以对出版物执行的操作的详细信息,请参阅 https://stackoverflow.com/a/18880927/586086