如何使用“组”在 pymongo 中对相似的行进行分组?
我对 mongodb/pymongo 很陌生。我已成功将数据导入 mongo,并希望使用 group 函数将相似的行分组在一起。例如,如果我的数据集如下所示:
data = [{uid: 1 , event: 'a' , time: 1} ,
{uid: 1 , event: 'b' , time: 2} ,
{uid: 2 , event: 'c' , time: 2} ,
{uid: 3 , event: 'd' , time: 4}
]
如何使用 group 函数根据 uid 字段对上述行进行分组,使得输出如下?
{ {uid: 1} : [{uid: 1 , event: 'a' , time: 1} , {uid: 1 , event: 'b' , time: 2} ],
{uid: 2} : [{uid: 2 , event: 'c' , time: 2} ],
{uid: 3} : [{uid: 3 , event: 'd' , time: 4} ] }
我通读了 http://www.mongodb.org/display/DOCS/Aggregation。然而,在我看来,这些例子总是聚合成一个数字或对象。
谢谢,
I am very new to mongodb/pymongo. I have successfully imported my data into mongo and would like to use the group function to group similar row together. For example, if my data set looks like this:
data = [{uid: 1 , event: 'a' , time: 1} ,
{uid: 1 , event: 'b' , time: 2} ,
{uid: 2 , event: 'c' , time: 2} ,
{uid: 3 , event: 'd' , time: 4}
]
How do I use the group function to group the above rows according to the uid field such that the output is as follows?
{ {uid: 1} : [{uid: 1 , event: 'a' , time: 1} , {uid: 1 , event: 'b' , time: 2} ],
{uid: 2} : [{uid: 2 , event: 'c' , time: 2} ],
{uid: 3} : [{uid: 3 , event: 'd' , time: 4} ] }
I read through the examples at http://www.mongodb.org/display/DOCS/Aggregation. However, it seems to me that those example always aggregate into a single number or object.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要使用
reduce
函数来实际减少任何东西。例如:我缩短了上面列表中的对象 ID 以提高可读性。
You needn't use the
reduce
function to actually reduce anything. For example:I've shortened the object IDs in the above listing to improve readability.