使用简化的数组数据发布游标

时间:2015-04-23 14:44:44

标签: mongodb meteor minimongo

我需要向用户发布简化版的帖子。每个帖子都包含一个'likes'数组,其中包含喜欢/不喜欢该帖子的所有用户,例如:

[
    { 
        _id: user_who_liked, 
        liked: 1 // or -1 for disliked 
    }, 
    ..
]

我正在尝试将简化版本发送给订阅数组的用户,该数组只包含他/她的喜欢:

Meteor.publish('posts', function (cat) {
  var _self = this;
  return Songs.find({ category: cat, postedAt: { $gte: Date.now() - 3600000 } }).forEach(function (post, index) {
    if (_self.userId === post.likes[index]._id) {
      // INCLUDE
    } else
      // REMOVE

    return post;
  })
});

我知道我可以改变结构,包括每个用户中的“喜欢”数据,但帖子通常设计得很短,最好将这些数据保存在每个帖子中。

1 个答案:

答案 0 :(得分:0)

您需要使用此特定语法查找包含window.onNotification = function onNotification(e)... 字段的帖子,其中包含至少一个包含字段的嵌入文档,其值为likes

this.userId

http://docs.mongodb.org/manual/tutorial/query-documents/#match-an-array-element

编辑:回答之前使用的是Meteor.publish("posts", function (cat) { return Songs.find({ category: cat, postedAt: { $gte: Date.now() - 3600000 }, "likes._id":this.userId },{ fields:{ likes:0 } }); }); ,这是不必要的,因为我们只需要在一个字段上进行过滤。

相关问题