如何使用mongo-template对数据进行分组 [英] How to group data using mongo-template

查看:157
本文介绍了如何使用mongo-template对数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须根据条件过滤数据,并将字段 isObtained 设置为true或false.然后,我尝试根据字段 grade 对数据进行分组.这是我的数据:

I have to filter data based on a criteria and set a field isObtained true or false. Then I am trying to group the data based on field grade. This is my data:

{
  "school": "xyz",
  "studentName": "John Doe",
  "grade": "first",
  "Hobbies": [
    "Painting",
    "Singing"
  ],
  "language": [
    "English"
  ],
"sport": "Badminton",
  "totalStudyHours": 85
},
{
  "school": xyz,
  "studentName": "Jane Doe",
  "grade": "third",
  "Hobbies": [
    "Painting",
    
  ],
  "language": [
    "Spanish"
  ],
"sport": "Karate",
  "totalStudyHours": 65
},
{
  "school": "xyz",
  "studentName": "joey",
  "grade": "first",
  "Hobbies": [
    "Singing"
  ],
  "language": [
    "English",
    "Italian"
  ],
"sport": "Cricket",
  "totalStudyHours": 75,
  "ispassed": true,
  
},
{
  "studentName": "jason",
  "grade": "third",
  "Hobbies": [
    "Painting",
    
  ],
  "language": [
    "English",
    "Spanish"
  ],
  "sport": "Tennis",
  "totalStudyHours": 95,
  "isObtained": true
},
{
  "studentName": "mike",
  "grade": "third",
  "Hobbies": [
    "Singing"
  ],
  "language": [
    "English",
    "Italian"
  ],
  "sport": "Badminton",
  "totalStudyHours": 70,
  "isObtained": true
}

预期输出是

[
  {
    "grade": "first",
    "values": [
      {
        "studentName": "John Doe",
        "grade": "first",
        "Hobbies": [
          "Painting",
          "Singing"
        ],
        "language": [
          "English"
        ],
        "sport": "Badminton",
        "totalStudyHours": 85,
        "isObtained": true
      },
      {
        "studentName": "joey",
        "grade": "first",
        "Hobbies": [
          "Singing"
        ],
        "language": [
          "English",
          "Italian"
        ],
        "sport": "Cricket",
        "totalStudyHours": 75,
        "isObtained": true
      }
    ]
  },
  {
    "grade": "third",
    "values": [
      {
        "studentName": "jason",
        "grade": "third",
        "Hobbies": [
          "Painting",
          
        ],
        "language": [
          "English",
          "Spanish"
        ],
        "sport": "Tennis",
        "totalStudyHours": 95,
        "isObtained": true
      },
      {
        "studentName": "mike",
        "grade": "third",
        "Hobbies": [
          "Singing"
        ],
        "language": [
          "English",
          "Italian"
        ],
        "sport": "Badminton",
        "totalStudyHours": 70,
        "isObtained": true
      }
    ]
  }
]

在mongoDb查询中,根据需要的字段设置 isObtained 字段.例如,如果我们希望将运动记录为羽毛球",那么当运动是羽毛球时, isObtained 字段将为true,否则为false.

In the mongoDb query, the isObtained field is set based on what field we want. For example, if we want records with sport as "Badminton" then the isObtained field will be true when sport is Badminton and false otherwise.

这是查询,但是在基于 grade 进行分组时,我遇到了问题.

Here is the query, but I am facing problem in grouping based on grade.

db.students.aggregate([
  {
    "$match": {
      "$and": [
        {
          "school": "xyz"
        }
      ]
    }
  },
  {
    "$project": {
      "sport": 1,
      "language": 1,
      "hobbies": 1,
      "isObtained": {
        "$and": [
          {
            "$eq": [
              "$sport",
              "Badminton"
            ]
          }
        ]
      }
    }
  }

推荐答案

  • $ match 您的条件
  • $ group 按年级排列,并以 values
  • 中的根文档的数组
  • 定义必填字段并检查创建条件的字段 isObtained ,如果 sport Badminton ,则为true,否则为false
    • $match your conditions
    • $group by grade and make array of root documents in values,
    • define required fields and check condition created field isObtained if sport is Badminton then true otherwise false
    • db.students.aggregate([
        { $match: { school: "xyz" } },
        {
          $group: {
            _id: "$grade",
            values: {
              $push: {
                sport: "$sport",
                language: "$language",
                Hobbies: "$Hobbies",
                isObtained: {
                  $cond: [{ $eq: ["$sport", "Badminton"] }, true, false]
                }
              }
            }
          }
        }
      ])
      

      游乐场

      如果要使用动态方法,请尝试将 $ mergeObjects $$ ROOT

      If you want to go with dynamic approach then try $mergeObjects with $$ROOT,

      游乐场

      这篇关于如何使用mongo-template对数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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