I'm having trouble implementing non-full text exclude search with golang and mongodb.
It's work in mongo shell:
db.collectionName.find({"comment":{"$not": /.*excludeThis.*/}})
It's don't work in Go:
package main
import (
"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
", result[k])
}
log.Println("-------")
}
I'm getting error:
Error: reflect.Value.Interface: cannot return value obtained from unexported field or method
Is here any way to make regex work or implement it in other way?