mongodb:匹配,按多个字段分组,项目和计数

麦华夫饼

因此,我正在学习mongodb,并且得到了一些作家培训。在这里,我试图通过按作者的国家和性别来对作品进行计数。到目前为止,这是我实现的目标:

db.writers.aggregate([
    { "$match": { "gender": {"$ne": male}}},
    { "$group": {
        "_id": {
            "country_id": "$country_id",
            "type": "$type"
        },
     }},
    { "$group": {
        "_id": "$_id.country_id",
        "literary_work": {
            "$push": { 
                "type": "$_id.type",
                "count": { "$sum": "$type" }
            }
        },
        "total": { "$sum": "$type" }
    }},
    { "$sort": { "country_id": 1 } },
    { "$project": {
        "literary_work": { "$slice": [ "$literary_work", 3 ] },
        "total": { "$sum": "$type" }
    }} 
])

可悲的是,我得到的输出不是我期望的:


 "_id" : GREAT BRITAIN,
    "literary_work" : [ 
        {
            "type" : "POEM",
            "count" : 0
        }, 
        {
            "type" : "NOVEL",
            "count" : 0
        }, 
        {
            "type" : "SHORT STORY",
            "count" : 0
        }
    ],
    "total" : 0

谁能告诉我在哪里插入计数阶段或我的错误是什么?)

更新:

数据样本:


{   
    "_id" : ObjectId("5f115c5d5f62f9f482cd7a49"),
    "author" : George Sand,
    "gender" : female,
    "country_id" : FRANCE,
    "title": "Consuelo",
    "type" : "NOVEL",
    
}

预期结果(注意!这是两个性别的结果):

{
    "_id" : FRANCE,
    "count" : 59.0,
    "literary_work" : [ 
        {
            "type" : "POEM",
            "count" : 14.0
        }, 
        {
            "type" : "NOVEL",
            "count" : 34.0
        }, 
        {
            "type" : "SHORT STORY",
            "count" : 11.0
        }
    ]
}
土生的

您的实现是正确的方法,但是缺少一些东西:

  • 首先错过了计数 $group
  • 在第一组计数的基础上,它可以计数整个计数 literary_work
  • 并且$project不需要您的查询

更正查询中的内容,

db.writers.aggregate([
  {
    $match: {
      gender: { $ne: "male" }
    }
  },
  {
    $group: {
      _id: {
        country_id: "$country_id",
        type: "$type"
      },
      // missed this
      count: { $sum: 1 }
    }
  },
  {
    $group: {
      _id: "$_id.country_id",
      // this count will be on the base of first group count
      count: { $sum: "$count" },
      literary_work: {
        $push: {
          type: "$_id.type",
          // add count in inner count
          count: "$count"
        }
      }
    }
  },
  // corrected from country_id to _id
  {
    $sort: { "_id": 1 }
  }
])

工作场所:https : //mongoplayground.net/p/JWP7qdDY6cc

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在MongoDB中按多个字段分组

来自分类Dev

在MongoDB中按多个字段分组

来自分类Dev

Linq - 按多个字段分组并计数(输出单个总数)

来自分类Dev

按多个字段计数

来自分类Dev

按多个字段和子字段过滤Mongo项目

来自分类Dev

ElasticSearch按多个字段分组

来自分类Dev

在MYSQL中按多个字段分组

来自分类Dev

SQL:按多个字段分组

来自分类Dev

每个对象按多个字段分组

来自分类Dev

在MongoDb中的多个字段上分组

来自分类Dev

MongoDB聚合按多个字段分组并获得平均值

来自分类Dev

如何使用Java在MongoDB中按字段对文档分组和计算计数

来自分类Dev

Javascript组平均值和按多个字段计数

来自分类Dev

在Spring MongoDB中,按一列分组,并获得另外两个字段的和

来自分类Dev

在SQL中使用分组依据获取多个字段的计数

来自分类Dev

按地图项分组并找到计数总和字段-Mongodb 3.6.9

来自分类Dev

按多个字段分组时,MySQL WITH ROLLUP

来自分类Dev

如何按多个字段对子文档数组进行分组?

来自分类Dev

按多个字段将Json数据分组

来自分类Dev

Elasticsearch按多个字段分组并汇总小时数(总计)

来自分类Dev

mysql:简化请求-按多个字段分组

来自分类Dev

Elasticsearch获取最新文档,按多个字段分组

来自分类Dev

按多个字段对数组进行分组

来自分类Dev

使用 Lodash 在 Nodejs 中按多个字段分组

来自分类Dev

MongoDB:聚合,按几个字段分组

来自分类Dev

如何汇总集合和按字段计数分组

来自分类Dev

MySQL子查询按特定字段分组和计数记录

来自分类Dev

Mysql按年份和计数分组字段

来自分类Dev

按嵌入字段分组并计数