【问题标题】:Meteor - Publish just the count for a collectionMeteor - 只发布一个集合的计数
【发布时间】:2015-03-12 22:30:42
【问题描述】:

是否可以只向用户发布集合的计数?我想在主页上显示总计数,但不将所有数据传递给用户。这是我尝试过的,但它不起作用:

Meteor.publish('task-count', function () {
    return Tasks.find().count();
});

this.route('home', { 
    path: '/',
    waitOn: function () {
        return Meteor.subscribe('task-count');
    }
});

当我尝试这个时,我得到一个无穷无尽的加载动画。

【问题讨论】:

  • 那些Tasks是属于用户还是什么?为什么我要问的是您可以在 users 集合本身中维护该计数并从那里获取它

标签: javascript meteor iron-router


【解决方案1】:

Meteor.publish 函数应该返回游标,但在这里您直接返回 Number,这是您的 Tasks 集合中的文档总数。

如果您想以正确的方式进行计数,则在 Meteor 中计数文档比看起来要困难得多:使用既优雅又有效的解决方案。

ros:publish-countstmeasday:publish-counts 的一个分支)使用fastCount 选项为小型集合(100-1000)或“几乎准确”的大型集合(数万)提供准确计数。

你可以这样使用它:

// server-side publish (small collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find());
});

// server-side publish (large collection)
Meteor.publish("tasks-count",function(){
  Counts.publish(this,"tasks-count",Tasks.find(), {fastCount: true});
});

// client-side use
Template.myTemplate.helpers({
  tasksCount:function(){
    return Counts.get("tasks-count");
  }
});

您将获得客户端响应计数以及服务器端性能合理的实现。

这个问题在(付费)防弹 M​​eteor 课程中讨论,推荐阅读:https://bulletproofmeteor.com/

【讨论】:

    【解决方案2】:

    我会使用Meteor.call

    客户:

     var count; /// Global Client Variable
    
     Meteor.startup(function () {
        Meteor.call("count", function (error, result) {
          count = result;
        })
     });
    

    在某个助手中返回count

    服务器:

    Meteor.methods({
       count: function () {
         return Tasks.find().count();
       }
    })
    

    *请注意,此解决方案不会是被动的。但是,如果需要反应性,则可以将其合并。

    【讨论】:

    • 有反应式的方法吗?
    【解决方案3】:

    这是一个老问题,但我希望我的回答可以帮助像我一样需要此信息的其他人。

    我有时需要一些杂项但反应性的数据来​​在 UI 中显示指标,文档计数就是一个很好的例子。

    1. 创建一个不会在服务器上导入的可重用(导出)客户端集合(以避免创建不必要的数据库集合)。请注意作为参数传递的名称(此处为“misc”)。
    import { Mongo } from "meteor/mongo";
    
    const Misc = new Mongo.Collection("misc");
    
    export default Misc;
    
    1. 在接受docId 和将保存计数的key 的名称的服务器上创建一个发布(使用默认值)。要发布到的集合名称 是用于创建仅客户端集合(“misc”)的集合。 docId 值无关紧要,它只需要在所有 Misc 文档中是唯一的,以避免冲突。有关发布行为的详细信息,请参阅 Meteor docs
    import { Meteor } from "meteor/meteor";
    import { check } from "meteor/check";
    import { Shifts } from "../../collections";
    
    const COLL_NAME = "misc";
    
    /* Publish the number of shifts that need revision in a 'misc' collection
     * to a document specified as `docId` and optionally to a specified `key`. */
    Meteor.publish("shiftsToReviseCount", function({ docId, key = "count" }) {
      check(docId, String);
      check(key, String);
    
      let initialized = false;
      let count = 0;
    
      const observer = Shifts.find(
        { needsRevision: true },
        { fields: { _id: 1 } }
      ).observeChanges({
        added: () => {
          count += 1;
    
          if (initialized) {
            this.changed(COLL_NAME, docId, { [key]: count });
          }
        },
    
        removed: () => {
          count -= 1;
          this.changed(COLL_NAME, docId, { [key]: count });
        },
      });
    
      if (!initialized) {
        this.added(COLL_NAME, docId, { [key]: count });
        initialized = true;
      }
    
      this.ready();
    
      this.onStop(() => {
        observer.stop();
      });
    });
    
    1. 在客户端,导入集合,确定docId 字符串(可以保存在常量中),订阅发布并获取适当的文档。瞧!
    import { Meteor } from "meteor/meteor";
    import { withTracker } from "meteor/react-meteor-data";
    import Misc from "/collections/client/Misc";
    
    const REVISION_COUNT_ID = "REVISION_COUNT_ID";
    
    export default withTracker(() => {
      Meteor.subscribe("shiftsToReviseCount", {
        docId: REVISION_COUNT_ID,
      }).ready();
    
      const { count } = Misc.findOne(REVISION_COUNT_ID) || {};
    
      return { count };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      • 2016-05-04
      • 2018-03-06
      • 2015-02-02
      • 2013-08-04
      • 2015-08-25
      • 2017-01-22
      相关资源
      最近更新 更多