我有一个数组,由MongoDB执行的map/reduce方法输出,它看起来像这样:

[{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>299.0}, 
{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>244.0}, 
{"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>1.0, "count"=>204.0}, 
{"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>510.0}, 
{"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>437.0}, 
{"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>469.0}, 
{"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>477.0}, 
{"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>481.0}, 
{"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>401.0}, 
{"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>468.0}, 
{"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>448.0}, 
{"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>0.0, "count"=>485.0}, 
{"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "type"=>10.0, "count"=>518.0}] 

您会注意到type有三个不同的值,在本例中是012,现在我们要做的是将这个散列数组按其type键的值分组,例如,这个数组的结果如下所示:

{
  :type_0 => [
    {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>299.0}, 
    {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>510.0}, 
    {"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>469.0}, 
    {"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>481.0}, 
    {"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>468.0}, 
    {"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>485.0}
  ],

  :type_1 => [
    {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>204.0}
  ],

  :type_10 => [
    {"minute"=>30.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>244.0}, 
    {"minute"=>45.0, "hour"=>15.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>437.0},
    {"minute"=>0.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>477.0}, 
    {"minute"=>15.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>401.0}, 
    {"minute"=>30.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>448.0}, 
    {"minute"=>45.0, "hour"=>16.0, "date"=>5.0, "month"=>9.0, "year"=>2011.0, "count"=>518.0}
  ]
} 

so I know these example arrays are really large, but I think it may be a more simple problem than I'm making it out to be

因此,基本上每个哈希数组都会根据其type键的值进行分组,然后作为哈希返回,每个类型都有一个数组,任何帮助都会非常有用,即使只是一些有用的提示也会非常感激.

推荐答案

array.group_by {|x| x['type']}

或者如果你想要符号,你甚至可以

array.group_by {|x| "type_#{x['type']}".to_sym}

我认为这最好地表达了"基本上每个array of hashes都是grouped by the value of its type key,然后是returned as a hash with an array for each type",即使它在输出哈希中只保留了:type键.

Mongodb相关问答推荐

如何在Mongo Aggregate Query中创建集合的对象ID数组,并在列表中查找另一个集合中的ID

MongoDB Aggregate:查找每个月的交叉日期范围的数量

MongoDB中的字符串聚合

在MongoDB Aggregate for My BooksDB中将`$match`放在`$unwin`之前或之后的区别

当日期和时间在不同键的字符串中时,Mongo 查询过滤今天的数据

如何在 mongodb 中将一个方面的结果合并到一个有条件的列表中?

如何使用 Golang 库获取 MongoDB 版本?

Raft Vs MongoDB 初选

使用绝对类型在 Typescript 中编写 Mongoose 的类型化模型和模式的类和接口

如何使用 Spring Data MongoDB 通过 GridFS ObjectId 获取二进制流

指定字段对于 MongoDB 是transient瞬态的,但对于 RestController 不是

mongodb 模式设计命名约定

你如何让 mongo 在远程服务器上运行?

Mongoimport json 文件更新或覆盖..?

MongoError: The dollar ($) prefixed field '$push' in '$push' is not valid for storage

Mongo聚合框架,排序然后分组不起作用

如何使用 mgo 从 golang 中的 mongodb 集合中 Select 所有记录

将新值推送到 mongodb 内部数组 - mongodb/php

用 MongoDB 中的属性表示多对多关系的最佳模型

在 MongoDB 中比较日期(moment.js)