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

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

如何使 $not regexp 查詢在 Go 中工作?

如何使 $not regexp 查詢在 Go 中工作?

Go
心有法竹 2023-06-05 17:07:01
我在使用 golang 和 mongodb 實現非全文排除搜索時遇到問題。它在 mongo shell 中工作:db.collectionName.find({"comment":{"$not": /.*excludeThis.*/}})它在 Go 中不起作用:package mainimport (    "log"    "regexp"    "github.com/night-codes/mgo-wrapper"    mgo "gopkg.in/mgo.v2")type (    SomeStruct struct {            ID      uint64 `form:"id" json:"id" bson:"_id"`            Name    string `form:"name" json:"name" bson:"name"`            Comment string   `form:"comment" json:"comment" bson:"comment"`        }    collectionStruct struct {        collection *mgo.Collection    }    obj map[string]interface{}    arr []interface{})var (    some = collectionStruct{collection: mongo.DB("somedb").C("somecollection")})func main() {    re := regexp.MustCompile(".*" + "exclude" + ".*")    query := obj{"comment": obj{"$not": re}}    result := []SomeStruct{}    if err := some.collection.Find(query).All(&result); err != nil {        log.Println("Error:", err)        return    }    log.Println("Result:")    for k := range result {        log.Printf("%+v\n", result[k])    }    log.Println("-------")}我收到錯誤:錯誤:reflect.Value.Interface:無法返回從未導出的字段或方法獲得的值這里有什么方法可以使正則表達式工作或以其他方式實現它嗎?
查看完整描述

1 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

答案是obj{"comment": obj{"$not": bson.RegEx{Pattern: ".*" + "exclude" + ".*"}}}代替obj{"comment": obj{"$not": re}}



查看完整回答
反對 回復 2023-06-05
  • 1 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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