猿问

使用golang和mgo,如何在MongoDB中搜索一系列值?

我在mgo主页上浏览了该示例,但是我一直在努力寻找一种查询一系列值的方法。该行:

searchResults, searchErr = SearchReading(bson.M{"k": key, "t": { $gte: start, $lte: end } }, limit)

失败并显示:

line67: syntax error: unexpected $

line67: missing type in composite literal


我省去了不必要的代码...


type Reading struct {

    K string  "k"

    T int64   "t"

    V float64 "v"

}


func SearchReading(q interface{}, limit int) (searchResults []Reading, searchErr string) {

    searchErr = ""

    searchResults = []Reading{}

    query := func(c *mgo.Collection) error {

        fn := c.Find(q).Limit(limit).All(&searchResults)

        if limit < 0 {

            fn = c.Find(q).All(&searchResults)

        }

        return fn

    }

    search := func() error {

        return withCollection("reading", query)

    }

    err := search()

    if err != nil {

        searchErr = "Database Error"

    }

    return

}


func GetReadingsForKey(key string, start int64, end int64, limit int) (searchResults []Reading, searchErr string) {

    searchResults, searchErr = SearchReading(bson.M{"k": key, "t": { $gte: start, $lte: end } }, limit)

    return

}


慕姐4208626
浏览 198回答 1
1回答

三国纷争

该行:searchResults,&nbsp;searchErr&nbsp;=&nbsp;SearchReading(bson.M{"k":&nbsp;key,&nbsp;"t":&nbsp;{&nbsp;$gte:&nbsp;start,&nbsp;$lte:&nbsp;end&nbsp;}&nbsp;},&nbsp;limit)需要更改为:searchResults,&nbsp;searchErr&nbsp;=&nbsp;SearchReading(bson.M{"k":&nbsp;key,&nbsp;"t":&nbsp;bson.M{"$gte":&nbsp;start,&nbsp;"$lte":&nbsp;end}},&nbsp;limit)
随时随地看视频慕课网APP

相关分类

Go
我要回答