MongoDb aggregate: select the whole group by x

I am trying to convert the following SQL-like statement to a mongo query using the new aggregation structure .

SELECT * FROM ...
GROUP BY class

So far I have managed to write the following, which works well, but only one field is selected / returned.

db.studentMarks.aggregate(
   {
     $project: {
        class : 1 // Inclusion mode
     }
   },
   {
     $group: {
        _id: "$class"
     }
   }
);

I also tried playing with $ project pipelines

The reason I need all the fields that need to be returned is because I will not know (in the future) which fields will be present or not. For instance. the “student” can have a field x, y, z or not. Thus, I need to “select everything”, group the results by one field and return it, for the general purpose - not necessarily for “student grades”, maybe for any type of data set without knowing all the fields.

, - - .

, - , .

+5
2

.

db.studentMarks.aggregate({
  $group: { _id: "$class" }
});
+1

, .

.

Option1

    `db.StudentMarks.aggregate( 
{
     $project: {
        class : 1 // Inclusion mode
     }
   }, 
{ $group : { 
        _id : "$class"
        ,field2: {$push: "$field2"}
        ,field3: {$push: "$field3"} 
        ,field4: {$push: "$field4"}
}}, $sort{field2:-1})`

, .

Option2

    `db.StudentMarks.aggregate( 
{
    { $group : { 
        _id : "$class"
        ,field2: {$push: "$field2"}
        ,field3: {$push: "$field3"} 
        ,field4: {$push: "$field4"}
}}, $sort{field2:-1})`

    `{
        "result" : [
                {
                        "_id" : 59,
                        "field2" : [
                                59,
                                59,
                                59
                        ],
                        "field3" : [
                                ObjectId("51e7072086eretetterr0001"),
                                ObjectId("51e7076786cb343453535354"),
                                ObjectId("51e716598435353534345335")
                        ],
                        "field4" : [
                                11111,
                                11111,
                                11111
                        ]
                },
                {
                        "_id" : 58,
                        "field2" : [
                                58,
                                58,
                                58,
                                58,
                                58
                        ],
                        "field3" : [
                                ObjectId("51e469ad3843534535353535"),
                                ObjectId("51e7178f86cb349845667775"),
                                ObjectId("51e71df286cb349842456gtg"),
                                ObjectId("51e71ea686cb345646466466"),
                                ObjectId("51e71eac86cb346546464646")
                        ],
                        "field4" : [
                                11111,
                                11111,
                                11111,
                                11111,
                                11111
                        ]
                }
        ],
        "ok" : 1
}`
0

All Articles