Olá, estou tentando fazer uma pesquisa com aggregation usando vatios estagios, mas está me retornando erro 500. Minha query a princípio esta certa, mas nao sei se estou utilizando corretamente o drive. Alguém consegue me ajudar? São duas funções. Basicamente, na funcao 1, eu estou dando um match por index, dando um replaceroot no obejeto com o lenguage que passei e pegando o objeto labels dentro dele. na na Função 2 eu aplico os filtros.
Função 1:
func ReadLabels(index int, language string) models.Label {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
var label models.Label
matchStage := bson.D{{"$match", bson.D{{"index", index}}}}
replaceRootStages := bson.D{{"$replaceRoot", bson.D{{"labels", language}}}}
projectStage := bson.D{{"$project", bson.D{{"limits", 0}}}}
pipeline := mongo.Pipeline{matchStage, replaceRootStages, projectStage}
answer, err := database.AnalyteCollection().Aggregate(ctx, pipeline, &options.AggregateOptions{})
if err = answer.All(ctx, &label); err != nil {
panic(err)
}
return label
}
Função 2:
func ReadLimits(index, age int, language, sex string) models.Limits {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
var limits models.Limits
matchStage := bson.D{{"$match", limits.Index == index}}
replaceRootStages := bson.D{{"$replaceRoot", bson.D{{"labels" + ".", language}}}}
unwindStage := bson.D{{"$unwind", "$use"}}
match2Stage := bson.D{{"$match", bson.D{{"limits.sex", sex}, {"limits.min_age", age}, {"limits.max_age", age}}}}
sortStage := bson.D{{"$sort", bson.D{{"rating", 1}, {"limits.index", 1}}}, {"$sort", bson.D{{"rating", 1}, {"limits.index", 1}}}}
groupStage := bson.D{{"$group", bson.D{{"_id", "$limits"}}}}
pipeline := mongo.Pipeline{matchStage, replaceRootStages, unwindStage, match2Stage, sortStage, groupStage}
answer, err := database.AnalyteCollection().Aggregate(ctx, pipeline, &options.AggregateOptions{})
if err = answer.All(ctx, &limits); err != nil {
panic(err)
}
return limits
}