MongoDB 聚合数据以生成“最新活动”

发布于 2024-10-03 12:07:13 字数 794 浏览 9 评论 0原文

我有一个 mongodb 集合,其中包含如下所示的文档:

[
  {
  :event => {:type => 'comment_created'}, 
  :item  => {:id => 10},
  :created_at => {:t => '11:19:03 +0100 2010', :d=> 'Fri, 19 Nov 2010'}
  }

,
  {
  :event => {:type => 'vote_created'}, 
  :item  => {:id => 10},
  :created_at => {:t => '11:19:03 +0100 2010', :d => 'Fri, 19 Nov 2010'}
  }
]

我需要的是构建一个“仪表板”,聚合每个项目的最新活动(当天)。结果应该类似于:

{
:item_id => 10,
:events => {
  :vote_created => [.. ordered list with latest 3 vote_created events/documents],
  :comment_created => [.. ordered list with latest 3 comment_created events/documents ],
  }
}

结果将用于构建“Facebook 风格”语法,例如:“Mike、John 和另外 3 个人今天对您的项目添加了评论。”

如何使用组或映射缩减函数聚合这些数据?

I have a mongodb collection that has documents like the ones below:

[
  {
  :event => {:type => 'comment_created'}, 
  :item  => {:id => 10},
  :created_at => {:t => '11:19:03 +0100 2010', :d=> 'Fri, 19 Nov 2010'}
  }

,
  {
  :event => {:type => 'vote_created'}, 
  :item  => {:id => 10},
  :created_at => {:t => '11:19:03 +0100 2010', :d => 'Fri, 19 Nov 2010'}
  }
]

What I need is to build a 'dashboard' aggregating latest activity (on current day) for each item. The result should be something like:

{
:item_id => 10,
:events => {
  :vote_created => [.. ordered list with latest 3 vote_created events/documents],
  :comment_created => [.. ordered list with latest 3 comment_created events/documents ],
  }
}

The result would be used to construct a 'Facebook-style' syntax like: 'Mike, John and 3 others added comments on your item today.'

How can I aggregate this data using a group or a map-reduce function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷途知返 2024-10-10 12:07:13

好的,有两种方法可以做到这一点:

方法#1:Map-Reduce

因此,首先,您需要运行一个 Map-Reduce,而不是一个组。

将 Map-Reduce 与“out”变量一起使用,这将生成一个新集合。然后,您将能够针对该新集合运行摘要查询。

您这样做的原因是您要求进行昂贵的查询,因此以“不完全”实时的方式访问它更为合理。

方法#2:双重写入

您基本上可以维护两个集合“详细信息”(顶部一个)和“摘要”(底部一个)。每当您写入详细信息时,也要对摘要进行更新。

MongoDB 有几种数组方法($push、$pull、$slice),应该使可以使“vote_created”数组保持最新。

首选项

您选择的方法完全取决于您拥有的架构类型以及您想要的用户体验。就我个人而言,我只会使用方法 #2 并继续附加到“vote_created”数组。我会将“Mike、John 和其他 3 个...”语法放在视图上的某个位置,因为它实际上是视图逻辑而不是数据库逻辑。

是的,方法#2 占用更多空间,但它也可以让您快速回答您经常提出的问题。所以你将不得不牺牲空间来获得这样的速度。

OK, there are two ways to do this:

Method #1: Map-Reduce

So first, you'll want to run a map-reduce, not a group.

Use Map-Reduce with the "out" variable which will generate a new collection. You'll then be able to run the summary queries against that new collection.

The reason you'll do this is that you're asking for an expensive query, so it's much more reasonable to access it in "not-quite" real-time.

Method #2: Double-writes

You can basically maintain two collections "details" (top one) and "summary" (bottom one). Whenever you do a write to the details, also perform an update to the summary.

MongoDB has several array methods ($push, $pull, $slice), that should make it possible to keep the "vote_created" array up-to-date.

Preferences

The method you select completely depends on the type of architecture you have and the user experience that you want. Personally, I would just use Method #2 and just keep appending to the "vote_created" array. I would put the 'Mike, John and 3 others...' syntax somewhere on the view, b/c it's really view logic not DB logic.

Yes method #2 takes more space, but it also gives you quick answers to the questions you ask alot. So you're going to have to sacrifice space to get that speed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文