草庐IT

mongodb 组和子组计数

coder 2023-10-27 原文

我有以下文档类型

{
    "_id" : "-fA2845ORqeyMUItKXfqZw",
    "user" : "553247ffdc8a4ade4bb09c5e",
    "state" : 2,
    "metadata" : {
        "language" : "en-US"
    },
    "pipeline" : {
        "api" : "http://localhost:4000",
        "provider" : 0
    },
    "ingest" : {
        "mimetype" : "audio/mpeg",
        "size" : 92794268,
        "client" : "computer",
        "isWriteable" : true
    },
    "assembly" : {
        "ok" : "ASSEMBLY_EXECUTING",
        "message" : "The assembly is currently being executed.",
        "assembly_id" : "23f29680b32911e59d48f358b85e112d",
        "parent_id" : null,
        "template_id" : null,
        "bytes_received" : 1265,
        "bytes_expected" : null,
        "upload_duration" : 0.079,
        "client_agent" : null,
        "client_referer" : null,
        "start_date" : "2016/01/04 21:21:38 GMT",
        "is_infinite" : false,
        "has_dupe_jobs" : false,
        "execution_start" : "2016/01/04 21:21:38 GMT",
        "execution_duration" : 0,
        "notify_start" : null,
        "notify_duration" : null,
        "last_job_completed" : null,
        "fields" : {

        },
    "export" : null,
    "job" : null,
    "transcript" : null,
    "status" : "transcoding",
    "created" : ISODate("2016-01-04T21:21:38.364Z"),
    "updated" : ISODate("2016-01-04T21:21:38.364Z")
}

我想得到文件扩展名的分割并按状态对它们进行分组

> db.files.aggregate([{"$match": {"created": { "$gte" : ISODate("2016-01-01T00:00:00Z")}}},{"$group" : {_id:"$status", count:{$sum:1}}}]);
{ "_id" : "transcoding error", "mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ]}
{ "_id" : "edited", "mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ] }
{ "_id" : "transcribed", "mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ] }
{ "_id" : "transcribing","mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ] }
{ "_id" : "received", "mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ] }
{ "_id" : "transcoding", "mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ]  }
{ "_id" : "blocked", "mimetype": ["audio/mpeg": 85, "audio/mp4":  65, .... ]  }

所以对于每个状态,我想按 "mimetype" 类型分组?

非常感谢任何建议

最佳答案

谢谢,我想我明白了

db.files.aggregate(
  {
    $group: {
      _id:   { status: "$status", mimetype: "$ingest.mimetype" },
      "mimetypes": { "$push": "$ingest.mimetype" },
      "total": { "$sum": 1 }
    }
  },
  {
    $group: {
      _id: { status: "$_id.status" },
      mimetype: { $addToSet: { mimetype: "$_id.mimetype", sum:"$total" } } 
    }
  }
);

返回

{ "_id" : { "status" : "transcoded" }, "mimetype" : [ { "mimetype" : "audio/mpeg", "sum" : 2 } ] }
{ "_id" : { "status" : "edited" }, "mimetype" : [ { "mimetype" : "audio/flac", "sum" : 5 }, { "mimetype" : "video/mp4", "sum" : 1982 }, { "mimetype" : "audio/x-ms-wma", "sum" : 185 }, { "mimetype" : "video/ogg", "sum" : 2 }, { "mimetype" : "audio/mp3", "sum" : 151 }, { "mimetype" : "audio/mp4", "sum" : 52 }, { "sum" : 146 }, { "mimetype" : "video/x-msvideo", "sum" : 14 }, { "mimetype" : "audio/wav", "sum" : 2106 }, { "mimetype" : "video/x-ms-wma", "sum" : 1 }, { "mimetype" : "audio/ogg", "sum" : 6 }, { "mimetype" : "audio/mpeg", "sum" : 2481 }, { "mimetype" : "audio/x-m4a", "sum" : 783 }, { "mimetype" : "application/octet-stream", "sum" : 34 }, { "mimetype" : "audio/amr", "sum" : 16 }, { "mimetype" : "audio/basic", "sum" : 2 }, { "mimetype" : "audio/x-aiff", "sum" : 41 }, { "mimetype" : "video/mpeg", "sum" : 7 }, { "mimetype" : "video/x-ms-wmv", "sum" : 31 }, { "mimetype" : "audio/aac", "sum" : 1 }, { "mimetype" : "video/quicktime", "sum" : 377 }, { "mimetype" : "audio/m4a", "sum" : 1 }, { "mimetype" : "video/3gpp", "sum" : 11 }, { "mimetype" : "video/x-flv", "sum" : 2 } ] }
{ "_id" : { "status" : "transcoding error" }, "mimetype" : [ { "mimetype" : "video/mp4", "sum" : 52 }, { "mimetype" : "audio/wav", "sum" : 36 }, { "mimetype" : "audio/aac", "sum" : 1 }, { "mimetype" : "audio/mpeg", "sum" : 20 }, { "mimetype" : "application/mxf", "sum" : 4 }, { "mimetype" : "audio/mp3", "sum" : 1 }, { "mimetype" : "video/mpeg", "sum" : 3 }, { "mimetype" : "video/x-ms-wmv", "sum" : 1 }, { "mimetype" : "audio/mp4", "sum" : 2 }, { "mimetype" : "video/quicktime", "sum" : 76 }, { "mimetype" : "image/jpeg", "sum" : 1 }, { "mimetype" : "application/octet-stream", "sum" : 9 }, { "sum" : 348 }, { "mimetype" : "video/x-matroska", "sum" : 3 }, { "mimetype" : "text/html", "sum" : 7 }, { "mimetype" : "audio/x-m4a", "sum" : 9 }, { "mimetype" : "audio/aiff", "sum" : 1 } ] }
{ "_id" : { "status" : "transcoding" }, "mimetype" : [ { "mimetype" : "audio/mpeg", "sum" : 20 }, { "mimetype" : "audio/ogg", "sum" : 1 }, { "sum" : 147 }, { "mimetype" : "image/jpeg", "sum" : 1 }, { "mimetype" : "audio/x-m4a", "sum" : 15 }, { "mimetype" : "audio/wav", "sum" : 22 }, { "mimetype" : "video/mp4", "sum" : 12 }, { "mimetype" : "audio/x-ms-wma", "sum" : 2 }, { "mimetype" : "video/quicktime", "sum" : 5 }, { "mimetype" : "application/mxf", "sum" : 1 }, { "mimetype" : "audio/mp3", "sum" : 4 } ] }
{ "_id" : { "status" : "received" }, "mimetype" : [ { "mimetype" : "video/x-ms-wmv", "sum" : 1 }, { "sum" : 16 }, { "mimetype" : "audio/wav", "sum" : 160 }, { "mimetype" : "video/3gpp", "sum" : 1 }, { "mimetype" : "audio/mp4", "sum" : 3 }, { "mimetype" : "video/quicktime", "sum" : 24 }, { "mimetype" : "video/mp4", "sum" : 2929 }, { "mimetype" : "audio/x-m4a", "sum" : 48 }, { "mimetype" : "audio/x-aiff", "sum" : 6 }, { "mimetype" : "audio/ogg", "sum" : 2 }, { "mimetype" : "audio/mp3", "sum" : 4 }, { "mimetype" : "audio/mpeg", "sum" : 199 }, { "mimetype" : "audio/flac", "sum" : 2 }, { "mimetype" : "audio/x-ms-wma", "sum" : 7 } ] }
{ "_id" : { "status" : "blocked" }, "mimetype" : [ { "mimetype" : "audio/wav", "sum" : 92 }, { "mimetype" : "audio/x-wav", "sum" : 20 }, { "mimetype" : "audio/mp4", "sum" : 3 }, { "mimetype" : "video/mp4", "sum" : 63 }, { "mimetype" : "audio/x-m4a", "sum" : 50 }, { "mimetype" : "application/octet-stream", "sum" : 1 }, { "mimetype" : "audio/mp3", "sum" : 40 }, { "mimetype" : "video/x-ms-wmv", "sum" : 1 }, { "mimetype" : "video/quicktime", "sum" : 8 }, { "mimetype" : "video/mpeg", "sum" : 2 }, { "sum" : 50 }, { "mimetype" : "audio/mpeg", "sum" : 163 }, { "mimetype" : "audio/basic", "sum" : 2 }, { "mimetype" : "audio/x-ms-wma", "sum" : 14 }, { "mimetype" : "audio/amr", "sum" : 3 }, { "mimetype" : "audio/x-aiff", "sum" : 3 } ] }
{ "_id" : { "status" : "transcribing" }, "mimetype" : [ { "mimetype" : "audio/mp3", "sum" : 55 }, { "mimetype" : "application/octet-stream", "sum" : 1 }, { "mimetype" : "audio/mp4", "sum" : 1 }, { "mimetype" : "audio/wav", "sum" : 58 }, { "mimetype" : "video/mp4", "sum" : 22 }, { "mimetype" : "audio/x-m4a", "sum" : 7 }, { "sum" : 10 }, { "mimetype" : "video/quicktime", "sum" : 3 }, { "mimetype" : "audio/x-aiff", "sum" : 8 }, { "mimetype" : "audio/x-ms-wma", "sum" : 1 }, { "mimetype" : "audio/mpeg", "sum" : 28 } ] }
{ "_id" : { "status" : null }, "mimetype" : [ { "sum" : 2 } ] }
{ "_id" : { "status" : "transcribed" }, "mimetype" : [ { "mimetype" : "audio/wav", "sum" : 3767 }, { "mimetype" : "video/quicktime", "sum" : 218 }, { "mimetype" : "audio/x-aiff", "sum" : 59 }, { "mimetype" : "video/x-ms-wmv", "sum" : 26 }, { "sum" : 280 }, { "mimetype" : "audio/x-m4a", "sum" : 691 }, { "mimetype" : "audio/x-ms-wma", "sum" : 119 }, { "mimetype" : "audio/mp3", "sum" : 186 }, { "mimetype" : "video/mp4", "sum" : 907 }, { "mimetype" : "audio/x-wav", "sum" : 1 }, { "mimetype" : "video/mpeg", "sum" : 7 }, { "mimetype" : "audio/amr", "sum" : 40 }, { "mimetype" : "audio/mp4", "sum" : 70 }, { "mimetype" : "application/octet-stream", "sum" : 26 }, { "mimetype" : "application/x-wav", "sum" : 1 }, { "mimetype" : "audio/caf", "sum" : 1 }, { "mimetype" : "audio/3gpp", "sum" : 1 }, { "mimetype" : "video/3gpp", "sum" : 21 }, { "mimetype" : "audio/ogg", "sum" : 4 }, { "mimetype" : "audio/mpeg", "sum" : 2464 }, { "mimetype" : "audio/aac", "sum" : 1 }, { "mimetype" : "audio/flac", "sum" : 3 }, { "mimetype" : "video/x-ms-wma", "sum" : 3 }, { "mimetype" : "audio/basic", "sum" : 1 }, { "mimetype" : "video/x-msvideo", "sum" : 3 }, { "mimetype" : "video/webm", "sum" : 2 } ] }

关于mongodb 组和子组计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39660473/

有关mongodb 组和子组计数的更多相关文章

  1. ruby-on-rails - Ruby on Rails 计数器缓存错误 - 2

    尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot

  2. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

  3. Ruby 计数数组对象,如果对象包含值 - 2

    我有一个数组:array=['Footballs','Baseball','football','Soccer']而且我需要计算看到Football或Baseball的次数,无论大小写和复数形式如何。这是我尝试做的,但没有成功:array.count{|x|x.downcase.include?'football'||x.downcase.include?'baseball'}编写这段代码的正确或更好的方法是什么?我正在寻找3作为答案。 最佳答案 我会将count与一个block结合使用,该block根据与您正在寻找的约束相匹配的正

  4. ruby - AWS 上远程机器上的进程计数 - 2

    我正在为在AmazonEC2实例上运行的应用程序设计一个AutoScaling系统。应用程序从SQS读取消息并对其进行处理。AutoScaling系统将监控两件事:SQS中的消息数量,所有EC2机器上运行的进程总数。例如,如果SQS中的消息数量超过3000,我希望系统自动缩放,创建一个新的EC2实例,在其上部署代码,当消息数量低于2000时,我希望系统终止EC2实例.我正在用Ruby和Capistrano做这件事。我的问题是:我无法找到一种方法来确定在所有EC2机器上运行的进程数并将该数字保存在变量中。你能帮帮我吗? 最佳答案 您可

  5. ruby-on-rails - FactoryGirl工厂特征内的序列不使用主序列计数器 - 2

    我有以下工厂:FactoryGirl.definedofactory:foodosequence(:name){|n|"Foo#{n}"}trait:ydosequence(:name){|n|"Fooy#{n}"}endendend如果我跑create:foocreate:foocreate:foo,:y我得到Foo1,Foo2,Fooy1。但我想要Foo1,Foo2,Fooy3。我怎样才能做到这一点? 最佳答案 经过smile2day'sanswer的一些提示后和thisanswer,我得出以下解决方案:FactoryGirl.

  6. ruby - 续集:如何使用分组和计数 - 2

    简单地说,我如何使用Sequel执行此查询?selecta.id,count(t.id)fromalbumsarightjointrackstont.album_id=a.idgroupbya.id 最佳答案 DB[:albums___a].right_join(:tracks___t,:album_id=>:id).select_group(:a__id).select_more{count(:t__id)} 关于ruby-续集:如何使用分组和计数,我们在StackOverflow上找

  7. ruby-on-rails - RSpec 检查数组的计数 - 2

    我正在测试我的ControllerAction以供练习。在我的Controller中,我只想从我的数据库中按名称获取所有不同的产品:defshop@products=Product.select('distincton(name)*').sort_by&:orderend我已经手动检查过了,它工作正常。现在我正在使用我的RSpec设置我的测试,我想测试@products是一个大于0的数组:RSpec.describePagesController,type::controllerdodescribe'GET#shop'doit'shouldgetallproudcts'doget:sh

  8. arrays - ruby 中的最佳排列计数算法 - 2

    我正在尝试计算由二进制形式的1和0的P数表示的数字的数量。如果P=2,则表示的数字为0011、1100、0110、0101、1001、1010,所以计数为6。我试过:[0,0,1,1].permutation.to_a.uniq但这不是大数的最佳解决方案(P可以什么可能是最好的排列技术,或者我们是否有任何直接的数学来做到这一点? 最佳答案 Numberofpermutationcanbecalculatedusingfactorial.a=[0,0,1,1](1..a.size).inject(:*)#=>4!=>24要计算重复项,

  9. ruby-on-rails - Rails 计数器缓存及其实现 - 2

    我正在尝试掌握Rails计数器缓存功能,但无法完全掌握它。假设我们有3个模型ABCA属于B或C,取决于字段key_type和key_id。key_type表示A属于B还是C,因此如果key_type="B"则记录属于B,否则属于C。在我的模型a.rb中,我定义了以下关联:belongs_to:b,:counter_cache=>true,:foreign_key=>"key_id"belongs_to:c,:counter_cache=>true,:foreign_key=>"key_id"和在b和c模型文件中has_many:as,:conditions=>{:key_type=>"

  10. sql - 在 Rails 中进行计数的正确方法是什么? - 2

    我有一个包含以下许多代码片段的Rails应用程序:Ouractivecommunityof我的问题是,这是计算观看次数的正确方法吗?看起来很“肮脏”,有没有更粗鲁的方法来计算?我可能在考虑命名范围,但我只是想确保这些类型的东西不会对性能产生更大的影响。谢谢你, 最佳答案 您不需要名称范围来执行计数。Account.where(:admin=>false).count但是命名作用域是使您的代码更具可重用性的绝佳方式。命名范围不会对您的应用程序产生任何明显的性能影响。 关于sql-在Rail

随机推荐