diff --git a/api/controllers/course.go b/api/controllers/course.go index 4264bbc..e4e3e6b 100644 --- a/api/controllers/course.go +++ b/api/controllers/course.go @@ -2,6 +2,7 @@ package controllers import ( "context" + "fmt" "net/http" "time" @@ -302,7 +303,7 @@ func courseProfessor(flag string, c *gin.Context) { if courseQuery, err = getQuery[schema.Course](flag, c); err != nil { return } - + fmt.Println("getCourseQuery error:", courseQuery) // determine the offset and limit for pagination stage and delete // "offset" field in professorQuery paginateMap, err := configs.GetAggregateLimit(&courseQuery, c) @@ -310,7 +311,7 @@ func courseProfessor(flag string, c *gin.Context) { respond(c, http.StatusBadRequest, "Error offset is not type integer", err.Error()) return } - + fmt.Println("getCourseQuery error:", paginateMap) // Pipeline to query the professors from the filtered courses courseProfessorPipeline := mongo.Pipeline{ // filter the courses @@ -344,7 +345,11 @@ func courseProfessor(flag string, c *gin.Context) { // replace the courses with professors bson.D{{Key: "$replaceWith", Value: "$professors"}}, - + bson.D{{Key: "$group", Value: bson.D{ // Groups all documents by professords _id + {Key: "_id", Value: "$_id"}, + {Key: "doc", Value: bson.D{{Key: "$first", Value: "$$ROOT"}}}, // Keeps first full document + }}}, + bson.D{{Key: "$replaceWith", Value: "$doc"}}, // keep order deterministic between calls bson.D{{Key: "$sort", Value: bson.D{{Key: "_id", Value: 1}}}}, @@ -352,7 +357,7 @@ func courseProfessor(flag string, c *gin.Context) { bson.D{{Key: "$skip", Value: paginateMap["latter_offset"]}}, bson.D{{Key: "$limit", Value: paginateMap["limit"]}}, } - + fmt.Println(courseProfessorPipeline) // perform aggregation on the pipeline cursor, err := courseCollection.Aggregate(ctx, courseProfessorPipeline) if err != nil {