亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 99 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號