Meteor - 仅发布集合的数量

14

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

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

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

this.route('home', { 
    path: '/',
    waitOn: function () {
        return Meteor.subscribe('task-count');
    }
});
当我尝试这样做时,我得到了一个无休止的加载动画。

这些Tasks属于用户还是其他什么?我之所以问是因为您可以在users集合中维护该计数,并从那里获取它。 - ajduke
3个回答

19

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/


7

I would use a Meteor.call

Client:

 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();
   }
})

*请注意,该解决方案不具有响应式。然而,如果需要响应性,则可以整合。


有没有响应式的方法? - Gobliins

1

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

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

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

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

export default Misc;

2. 在服务器上创建一个发布程序,接受docId和将保存计数的key名称(具有默认值)。要发布到的集合名称是仅用于创建客户端集合的那个集合(“misc”)。docId的值并不重要,它只需要在所有Misc文档中是唯一的,以避免冲突。有关发布行为的详细信息,请参见Meteor文档
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 };
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接