无法完全理解the docs的例子...我试着用各种不同的方式运行它,这样我就可以观察它是如何工作的,等等.

你怎么订阅这个?我们是否可以包含实现此功能所需的客户端代码?

有叫messages-count的Collection 吗?Room是信息的集合吗?我们可以在示例中包含集合定义吗?

任何关于这方面的建议都会很好!

NOTE:这是这个问题最初发布(2012年5月)时出现的代码.现在更简单了.

// server: publish the current size of a collection
Meteor.publish("messages-count", function (roomId) {
  var self = this;
  var uuid = Meteor.uuid();
  var count = 0;

  handle = Room.find({room_id: roomId}).observe({
    added: function (doc, idx) {
      count++;
      self.set("messages-count", uuid, "count", count);
      self.flush();
    },
    removed: function (doc, idx) {
      count--;
      self.set("messages-count", uuid, "count", count);
      self.flush();
    }
    // don't care about moved or changed
  });

  // remove data and turn off observe when client unsubs
  self.onStop(function () {
    handle.stop();
    self.unset("messages-count", uuid, "count");
    self.flush();
  });
});

推荐答案

谢谢你提醒我写一个更清楚的解释.下面是一个更完整的例子,并附上我的 comments .我已经清理了一些错误和不一致之处.下一个docs版本将使用这个.

Meteor.publish是相当灵活的.它不仅限于将现有MongoDB集合发布到客户端:我们可以发布任何我们想要的内容.具体地说,Meteor.publish定义了客户机可以订阅的set of documents.每个文档都属于某个集合名称(一个字符串),有一个唯一的_id字段,然后有一组JSON属性.当集合中的文档发生更改时,服务器会将更改发送到每个订阅的客户端,使客户端保持最新.

我们将在这里定义一个名为"counts-by-room"的文档集,它包含一个名为"counts"的集合中的单个文档.该文档将有两个字段:带有房间ID的roomIdcount:该房间中的消息总数.没有真正的名为counts的MongoDB集合.这只是我们的Meteor服务器将要发送到客户端的集合的名称,并存储在名为countsclient-side个集合中.

为此,我们的publish函数接受一个roomId参数,该参数将来自客户端,并观察该房间中所有消息(在别处定义)的查询.我们可以在这里使用更高效的observeChanges形式来观察查询,因为我们不需要完整的文档,只需要知道添加或删除了一个新文档.每当我们感兴趣的roomId条新消息被添加时,我们的回调都会增加内部计数,然后用更新后的总数向客户端发布一个新文档.当一条消息被删除时,它会减少计数并向客户端发送更新.

当我们第一次调用observeChanges时,对于已经存在的每条消息,将立即运行added次回调.然后,无论何时添加或删除消息,future 的更改都会触发.

我们的发布功能还注册了一个onStop处理程序,以便在客户端取消订阅时(手动或断开连接时)进行清理.这个处理程序从客户端删除属性,并删除正在运行的observeChanges.

每次新客户端订阅"counts-by-room"时,发布函数都会运行,因此每个客户端都会有一个observeChanges代表其运行.

// server: publish the current size of a collection
Meteor.publish("counts-by-room", function (roomId) {
  var self = this;
  var count = 0;
  var initializing = true;

  var handle = Messages.find({room_id: roomId}).observeChanges({
    added: function (doc, idx) {
      count++;
      if (!initializing)
        self.changed("counts", roomId, {count: count});  // "counts" is the published collection name
    },
    removed: function (doc, idx) {
      count--;
      self.changed("counts", roomId, {count: count});  // same published collection, "counts"
    }
    // don't care about moved or changed
  });

  initializing = false;

  // publish the initial count. `observeChanges` guaranteed not to return
  // until the initial set of `added` callbacks have run, so the `count`
  // variable is up to date.
  self.added("counts", roomId, {count: count});

  // and signal that the initial document set is now available on the client
  self.ready();

  // turn off observe when client unsubscribes
  self.onStop(function () {
    handle.stop();
  });
});

现在,在客户机上,我们可以像对待典型的Meteor订阅一样对待它.首先,我们需要一个Mongo.Collection,将保存我们的计算计数文件.由于服务器正在发布到名为"counts"的集合中,因此我们将"counts"作为参数传递给Mongo.Collection构造函数.

// client: declare collection to hold count object
Counts = new Mongo.Collection("counts");

然后我们可以订阅.(实际上,你可以在声明Collection 之前订阅:Meteor将对传入的更新进行排队,直到有地方放置它们.)subscription的名称是"counts-by-room",它有一个参数:当前房间的ID.我将其包装在Deps.autorun中,这样当Session.get('roomId')发生变化时,客户将自动取消订阅旧房间的计数,并重新订阅新房间的计数.

// client: autosubscribe to the count for the current room
Tracker.autorun(function () {
  Meteor.subscribe("counts-by-room", Session.get("roomId"));
});

最后,我们得到了Counts个文档,我们可以像客户机上的任何其他Mongo集合一样使用它.每当服务器发送新计数时,引用此数据的任何模板都将自动重新绘制.

// client: use the new collection
console.log("Current room has " + Counts.findOne().count + " messages.");

Mongodb相关问答推荐

使用mongosh将大型json文件插入到mongo集合中

Mongodb如果数组中有外部键,则将排序应用到查找结果

如何在MongoDB中对两个数组进行分组?

根据参考图计算子文档列表的最大值

可变用户 Select

在mongodb中,如何使用聚合来获取字段之间的对应关系

在 MongoDB 中,如何使用对当前值进行操作的函数来更新嵌套文档的值?

使用新字段插入数据或使用 updateOne mongodb 有条件地更新

mongo shell 命令不接受使用 db 命令

有谁知道这个错误的修复方法(TypeError: Cannot assign to read only property ‘map’ of object '#')

mongoDB vs mySQL - 为什么在某些方面比另一个更好

哪个库最适合用于带有 Scala 的 MongoDB?

.insertOne 不是函数

将 MongoDB 地理空间索引与 3d 数据结合使用

使用 EventSourcing(NodeJS、MongoDB、JSON)跨多个偶尔连接的客户端同步数据

如何使用 mongoexport 导出排序数据?

如何在 Ruby on Rails 环境中使用 Mongoid 进行通配符搜索?

mongo - 如何查询嵌套的 json

缩短 MongoDB 属性名称值得吗?

如何在 Ubuntu 10.04 中使用 --auth 选项重新启动 mongodb?