Meteor - 发布集合的计数

时间:2015-01-14 16:41:00

标签: javascript meteor iron-router

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

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

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

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

3 个答案:

答案 0 :(得分:16)

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

如果你想以正确的方式做到这一点,在Meteor中计算文件是一个非常困难的任务:使用优雅而有效的解决方案。

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

你可以这样使用它:

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

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

这个问题在(付费)防弹Meteor课程中讨论,建议阅读:https://bulletproofmeteor.com/

答案 1 :(得分:6)

我会使用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();
   }
})

*请注意,此解决方案不会被动反应。但是,如果需要反应,可以加入。

答案 2 :(得分:0)

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

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

  1. 创建一个不会在服务器上导入的可重用(导出)的客户端专用集合(以避免创建不必要的数据库集合)。请注意作为参数传递的名称(此处为“ misc”)。
import { Mongo } from "meteor/mongo";

const Misc = new Mongo.Collection("misc");

export default Misc;
  1. 在服务器上创建一个接受docId和将存储计数的key名称的发布(具有默认值)。要发布到的集合名称 是用于创建仅客户端集合(“ misc”)的集合。 docId的值无关紧要,只需要在所有其他文档中保持唯一即可避免冲突。有关发布行为的详细信息,请参见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 };
});