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

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

Mongodb 如何通過正則表達式或在許多字段上進行搜索?

Mongodb 如何通過正則表達式或在許多字段上進行搜索?

Go
胡子哥哥 2022-12-26 16:44:23
我想在 mongodb 的許多領域進行搜索。這是我到目前為止所做的var filter bson.Dfilter = append(filter, bson.E{"title", primitive.Regex{Pattern: "input", Options: "i"}})filter = append(filter, bson.E{"author", primitive.Regex{Pattern: "input", Options: "i"}})filter = append(filter, bson.E{"content", primitive.Regex{Pattern: "input", Options: "i"}})cur, err := p.postProposalCollection.Find(ctx, filter)但是它是AND這樣工作的WHERE title ~ 'input' AND author ~ 'input' AND content ~ 'input'我希望它像這樣工作WHERE title ~ 'input' OR author ~ 'input' OR content ~ 'input'
查看完整描述

1 回答

?
UYOU

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

也許你可以直接使用$orand$regex


filter := bson.M{

    "$or": bson.A{

        bson.M{"title": bson.M{"$regex": "input"}},

        bson.M{"author": bson.M{"$regex": "input"}},

        bson.M{"content": bson.M{"$regex": "input"}},

    },

}


p.postProposalCollection.Find(ctx, filter)


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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