为了账号安全,请及时绑定邮箱和手机立即绑定

golang mongodb如何同时使用count和or表达式?

golang mongodb如何同时使用count和or表达式?

Go
莫回无 2022-10-24 15:37:38
我写了这行代码,但是当我尝试它时,它只是在总大于我给出的数字时返回,它只是不使用检查。我的意思是我有 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})


查看完整回答
反对 回复 2022-10-24
  • 1 回答
  • 0 关注
  • 81 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号