【问题标题】:In Meteor Collections, use an array-field or another collection?在 Meteor Collections 中,使用数组字段还是其他集合?
【发布时间】:2013-08-07 03:26:40
【问题描述】:

问题

简而言之,我的问题是:当文档中的数组发生更改时,用户会收到新数组,还是仅收到更改?

如果这个问题不清楚,我在下面描述了我的问题。

问题

我有一个集合,其文档包含一个数组字段,两个用户将向其中推送值。此集合中的文档如下所示:

var document = {
    userId1: "...user id...", // The id of the first of the two users.
    userId2: "...user id...", // The id of the second of the two users.
    data: [] // The field the two users will push values to.
}

data 一开始是空的,然后用户将轮流向它推送值。

当其中一个用户向data 推送某个值时,服务器会将更改发送给第二个用户。第二个用户会收到整个data-array,还是只收到更改(推送的值)?我有点担心第二个用户会收到整个data-array,即使它只是一个被推送到它的值,如果data包含很多值,我担心这会成为一个瓶颈.

是这样吗?如果是这样,使用另一个集合来存储值将解决它,对吗?像这样的:

var document = {
    id: "...unique id...",
    userId1: "...user id...", // The id of the first of the two users.
    userId2: "...user id..." // The id of the second of the two users.
}

var documentData = {
    idReference: "...the unique id in the document above...", 
    value: "...a value..."
}

不要将值推入document 中的数组,而是将它们插入包含documentData 的集合中。这(我知道)不会有我担心第一个解决方案的缺点(但如果它没有缺点,我宁愿使用第一个解决方案)。

【问题讨论】:

  • 您可以通过关注these instructions 来查看 websocket 连接中的帧,从而仔细检查到底发生了什么。

标签: arrays mongodb collections meteor field


【解决方案1】:

根据https://github.com/meteor/meteor/blob/master/packages/livedata/DDP.md

  • changed(服务器 -> 客户端):
    • collection:string(集合名称)
    • id: string(文档 ID)
    • fields:带有 EJSON 值的可选对象
    • cleared:可选的字符串数组(要删除的字段名)

用户将收到新数组。要仅发送“差异”,请使用 {userId: userId, value: value} 文档的集合。

【讨论】:

    【解决方案2】:

    我检查了 user728291 评论发送的内容,似乎发送了整个数组字段,而不仅仅是推送的值。但我将使用另一个集合而不是数组字段来解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2018-02-12
      • 2017-01-06
      相关资源
      最近更新 更多