我寫了這行代碼,但是當我嘗試它時,它只是在總大于我給出的數字時返回,它只是不使用檢查。我的意思是我有 6 個文檔,其中 5 個是真實的,但它給了我 6 個結果!checked := bson.D{{"$or", []interface{}{bson.D{{"checked", false}}, bson.D{{"checked", nil}}}}}totalReport := bson.D{{"total", bson.D{{"$gte", config.ReportNumberToChangeNickname}}}}totalReportAndChecked := bson.D{{"$and", []interface{}{checked, totalReport}}}matchStage := bson.D{{"$match", totalReportAndChecked}}groupStage := bson.D{{"$group", bson.D{{"_id", "$user_id"}, {"total", bson.D{{"$sum", 1}}}}}}cursor, err := UserReportDb.Aggregate(ctx, mongo.Pipeline{groupStage, matchStage})if err != nil { fmt.Println(err) return &mongo.Cursor{}, err}
1 回答

繁星點點滴滴
TA貢獻1803條經驗 獲得超3個贊
我找到了答案謝謝
cond := bson.D{{"$cond", []interface{}{"$checked", 0, 1}}}
matchStage := bson.D{{"$match", bson.D{{"$expr", bson.D{{"$gte", []interface{}{"$total", config.ReportNumberToChangeNickname}}}}}}}
groupStage := bson.D{{"$group", bson.D{{"_id", "$user_id"}, {"total", bson.D{{"$sum", cond}}}, {"value", bson.D{{"$last", "$value"}}}}}}
cursor, err := UserReportDb.Aggregate(ctx, mongo.Pipeline{groupStage, matchStage})
- 1 回答
- 0 關注
- 99 瀏覽
添加回答
舉報
0/150
提交
取消