Meteor - 只发布集合的计数

Meteor - Publish just the count for a collection

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

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

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

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

我会用 Meteor.call

客户:

 var count; /// Global Client Variable

 Meteor.startup(function () {
    Meteor.call("count", function (error, result) {
      count = result;
    })
 });

return count 在一些助手中

服务器:

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

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

Meteor.publish 函数应该 return 游标,但在这里您 return 直接 Number 这是 [=13= 中的文档总数] collection.

在 Meteor 中对文档进行计数是一项比看起来更困难的任务,如果您想以正确的方式进行:使用既优雅又有效的解决方案。

ros:publish-counts (a fork of tmeasday:publish-counts) 提供小 collections (100-1000) 的准确计数或 "nearly accurate" 较大 collections (数万) 的计数使用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");
  }
});

您将获得 client-side 反应计数以及 server-side 合理的性能实施。

此问题在(付费)防弹流星课程中讨论,推荐阅读:https://bulletproofmeteor.com/

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

有时我需要一些杂项但反应性数据来显示 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 };
});