MongoDB Map-Reduce从每个用户的最后一条记录中查找值 [英] MongoDB Map-Reduce Find Values from Last Record per User

查看:126
本文介绍了MongoDB Map-Reduce从每个用户的最后一条记录中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我掌握的最新数据来获取有关Twitter帖子作者的一些数据

I am trying to get some data about the authors of twitter posts, based on the latest data I have

考虑到Twitter帖子的集合,我想从每个作者的最新帖子中获取信息-即.我希望每个作者都能获得好友数.

Given a collection of twitter posts, I want to pull information from the latest post per author - ie. I want per author to get the friend count.

该集合大致具有这样的数据.

Roughly the collection has data like this.

[{"post":
{"post_date":"2012年3月24日星期六05:52:21 +0000" {"author":{"author_id":123,"friend_count":321}} ,{"post_date":"2012年3月17日星期六03:22:11 +0000" {"author":{"author_id":123,"friend_count":311}} ,{"post_date":星期六,2012年3月10日03:22:11 +0000" {"author":{"author_id":123,"friend_count":331}}}}]

[{"post":
{"post_date": "Sat, 24 Mar 2012 05:52:21 +0000" {"author": {"author_id":123, "friend_count":321}} ,{"post_date": "Sat, 17 Mar 2012 03:22:11 +0000" {"author": {"author_id":123, "friend_count":311}} ,{"post_date": "Sat, 10 Mar 2012 03:22:11 +0000" {"author": {"author_id":123, "friend_count":331}} }}]

我不希望friend_count的最大值,而是最新帖子中的值.

I don't want the max of the friend_count but the value from the latest post.

感谢

推荐答案

您不需要使用mapreduce,只需进行简单的聚合即可.

You don't need to use mapreduce, you can do this with simple aggregation.

以下方面的作用

db.collection.aggregate(
         {$sort:{post_date:-1}}, 
         {$group:{_id:"$author.author_id", friend_count:{$first:"$author.friend_count"}}}
)

鉴于您提供的简化示例数据将按照post_date最新至最旧的顺序$sort对其进行排序,这样,当按author_id分组时,$first记录将是最新记录.

Given the simplified sample data you gave this will $sort it by post_date newest to oldest, so that then when grouping by author_id, the $first record will be one that's the latest.

这篇关于MongoDB Map-Reduce从每个用户的最后一条记录中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆