【问题标题】:Returning multiple cursors on Meteor.js在 Meteor.js 上返回多个光标
【发布时间】:2016-01-12 18:08:13
【问题描述】:

这是我的重点:用户可以访问不同的聊天室。当他们这样做时,如果尚未添加 ID,我会将聊天室 ID 推送到客户端的反应数组中。如果没有,我会重新运行一个自动运行,它将订阅客户在此会话中访问的每个聊天室的最后 50 条消息。

但我希望他只能收到每个聊天室的最后 50 条消息!

以前我有:

Meteor.publish("getChatMess", function(cids) {
    if (!Array.isArray(cids))
        cids = [cids];

    return (ChatMess.find({ chatId: cids[i] }, { sort: { date: -1 }, limit: (75 * cids.length) } ));
});

这显然不符合我的需要,所以我这样做了:

Meteor.publish("getChatMess", function(cids) {
    if (!Array.isArray(cids))
        cids = [cids];


    let i = -1,
        cursors = [];

    while (cids[++i])
    {
        cursors.push(ChatMess.find({ chatId: cids[i] }, { sort: { date: -1 }, limit: 50 } ));
    }

    return (cursors);
});

但是,Meteor 给我一个错误,因为我无法在一次推送中从同一个集合中返回多个光标。

我该如何解决?我知道我可以检查每个聊天,选择每个聊天的最后 50 条消息(或更少)的 id,并查询所有相应 ID 的 chatMess 集合,但是对于我想要的东西来说,它会非常繁重且未优化,特别是如果客户在会话中已经访问了几十个聊天室。

【问题讨论】:

    标签: javascript node.js mongodb meteor


    【解决方案1】:

    meteor docs中所述

    如果你在一个数组中返回多个游标,它们当前必须都是 来自不同的收藏。我们希望取消这一限制 未来版本。

    但是有一个叫做 smart-publish 的包可以帮助你管理来自同一个集合的多个游标。这可能会有所帮助https://atmospherejs.com/mrt/smart-publish

    【讨论】:

    • 真遗憾,我什至没有想过要检查气氛...谢谢! :)
    • 你为什么不能只做 ChatMess.find({ chatId: {$in cids} } ?
    【解决方案2】:

    随便用

    Meteor.publish("getChatMess", function(cids) {
        if (!Array.isArray(cids)) cids = [cids];
        return ChatMess.find({chatId: {$in: cids}});
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多