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

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

如果 slice 存在并且它包含至少一個設置為 true 的特定變量

如果 slice 存在并且它包含至少一個設置為 true 的特定變量

Go
aluckdog 2022-07-11 16:23:02
我正在嘗試檢查訂閱部分是否存在,以及它是否包含至少一個名為 premium 的變量,其值為 true。如果是,它應該返回,如果不是,它不應該返回。目前它正在返回集合中的對象,即使該值設置為 false。// query to find all the users accounts that have purchased premium subscriptionshasPurchasedSubscriptions := c.QueryParam("hasPurchasedSubscriptions")if hasPurchasedSubscriptions != "" {    pipeline = append(pipeline, bson.M{        "$match": bson.M{"$and": []interface{}{            bson.M{"subscriptions": bson.M{"$exists": true}},            bson.M{"subscriptions": bson.M{"$elemMatch": bson.M{"premium": true}}},        }},    })}})
查看完整描述

1 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

只需使用:


pipeline = append(pipeline, bson.M{

    "$match": bson.M{

        "subscriptions": bson.M{"$elemMatch": bson.M{"premium": true}},

    },

})

不需要檢查它是否存在,它必須,否則它不能有一個帶有premium=true.


如果您對元素只有這一個條件,您還可以將其簡化為:


pipeline = append(pipeline, bson.M{

    "$match": bson.M{"subscriptions.premium": true},

})


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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