简体   繁体   English

在Meteor(MongoDB)中按另一个集合中的值搜索一个集合

[英]Searching one collection by a value in another, in Meteor (MongoDB)

I'm building a Twitter clone, and I've hit a wall with sorting. 我正在构建一个Twitter克隆,并且已经在排序上碰壁了。

Here are my collections: 这是我的收藏:

Tweets:
  createdAt: Date
  body:      String
  userId:    String

Events:
  createdAt: Date
  type:      String (either 'retweet' or 'like')
  tweetId:   String
  userId:    String

When a user clicks 'retweet', it creates a new Event document which tracks the tweet and user IDs. 当用户单击“转发”时,它将创建一个新的Event文档,该文档将跟踪推文和用户ID。

Then, when I view a user's profile page, I do a search for all Tweets either composed by the user, or where a 'retweet' Event exists with that user Id. 然后,当我查看用户的个人资料页面时,我会搜索该用户撰写的所有Tweets,或者与该用户ID一起存在“ retweet”事件的地方。 I do a reactive join on the server so that the Tweets collection has all the necessary tweets. 我在服务器上进行了响应式联接,以便Tweets集合具有所有必需的tweets。

The problem is sorting: I'm sorting all Tweets by their createdAt value. 问题是排序:我正在按它们的createdAt值对所有Tweet进行排序。 If I retweet a Tweet that was created yesterday, it'll show up in my feed below any of my own tweets in the last day. 如果我转推昨天创建的推文,则该推文会在最后一天显示在我的提要任何我自己的推文下方

In other words, I need a way to sort Tweets either by their own createdAt field, or by the createdAt field of an associated Event if such a thing exists. 换句话说,我需要一种方法,可以通过推文本身的createdAt字段相关事件的createdAt字段(如果存在)对Tweet进行排序。


I'm not opposed to restructuring my models if there's a better way to do this. 如果有更好的方法,我不反对重组模型。 I had thought about creating a new Tweet whenever you 'retweet', but the problem is that each Tweet has a likeCount and a retweetCount . 我曾考虑过在您“转发”时创建一个新的Tweet,但是问题是每个Tweet都有一个likeCount和一个retweetCount If I create a brand new Tweet whenever it's retweeted, and someone likes or retweets the retweet, all the numbers would be wrong (or there would be an absurd amount of denormalizing-updating required). 如果我在转发时创建了一个全新的Tweet,并且有人喜欢或转发了该转发,则所有数字都将是错误的(否则将需要大量的非规范化更新)。

  1. Make sure you always create an event for each tweet 确保您始终为每个推文创建一个event
  2. Use the [reywood:publish-composite] https://atmospherejs.com/reywood/publish-composite ) package to publish the Events collection sorted by the Event.createdAt key and limited to however many events you want to show. 使用[reywood:publish-composite] https://atmospherejs.com/reywood/publish-composite包来发布按Event.createdAt键排序的Events集合,并且该Events集合仅限于要显示的事件数。
  3. In your template, do {{#each events}} instead of {{each tweets}} 在模板中,执行{{#each events}}而不是{{each tweets}}
  4. Use a template helper to return the body of the tweet 使用模板助手返回推文的正文

Helper: 帮手:

Template.myTemplate.helpers({
  events: function(){
    return Events.find({},{sort: {createdAt: -1}});
  },
  body: function(){
    return Tweets.findOne({ _id: this.tweetId }).body;
  }
});

This would be a normalized approach. 这将是一种规范化的方法。 For performance and given that tweets are short you can just copy the body field from each tweet into the Events document. 为了提高性能,并考虑到tweet简短,您可以将每个tweet的body字段复制到Events文档中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM