使用golang和mgo,我如何在MongoDB中搜索一定范围内的值?

huangapple go评论92阅读模式
英文:

Using golang and mgo, how do I search for a range of values in 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
}
英文:

I worked through the example on the mgo homepage, but I'm struggling to find a way to query a range of values. The line:
searchResults, searchErr = SearchReading(bson.M{&quot;k&quot;: key, &quot;t&quot;: { $gte: start, $lte: end } }, limit)
fails with:
line67: syntax error: unexpected $
line67: missing type in composite literal

I left out the non-essential bits of code...

type Reading struct {
	K string  &quot;k&quot;
	T int64   &quot;t&quot;
	V float64 &quot;v&quot;
}

func SearchReading(q interface{}, limit int) (searchResults []Reading, searchErr string) {
	searchErr = &quot;&quot;
	searchResults = []Reading{}
	query := func(c *mgo.Collection) error {
		fn := c.Find(q).Limit(limit).All(&amp;searchResults)
		if limit &lt; 0 {
			fn = c.Find(q).All(&amp;searchResults)
		}
		return fn
	}
	search := func() error {
		return withCollection(&quot;reading&quot;, query)
	}
	err := search()
	if err != nil {
		searchErr = &quot;Database Error&quot;
	}
	return
}

func GetReadingsForKey(key string, start int64, end int64, limit int) (searchResults []Reading, searchErr string) {
	searchResults, searchErr = SearchReading(bson.M{&quot;k&quot;: key, &quot;t&quot;: { $gte: start, $lte: end } }, limit)
	return
}

答案1

得分: 12

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

英文:

The line:

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

needs to change to:

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

huangapple
  • 本文由 发表于 2012年10月31日 20:48:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/13158335.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定