Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

mongodb - How to group data using mongo-template

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
}

The expected output is

[
  {
    "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
      }
    ]
  }
]

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.

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"
            ]
          }
        ]
      }
    }
  }
question from:https://stackoverflow.com/questions/65914720/how-to-group-data-using-mongo-template

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • $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]
          }
        }
      }
    }
  }
])

Playground


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

Playground


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...