dounieqi6959 2019-02-02 18:41
浏览 78
已采纳

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

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?

  • 写回答

1条回答 默认 最新

  • dongtangjie0495 2019-02-03 18:43
    关注

    The answer is obj{"comment": obj{"$not": bson.RegEx{Pattern: ".*" + "exclude" + ".*"}}} instead of obj{"comment": obj{"$not": re}}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn