在使用Go的mongo-driver运行Find().All()时遇到的问题。

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

Issues running Find().All() using go mongo-driver

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对MongoDB还不熟悉,目前我们正在尝试将旧的mgo驱动迁移到go mongo-driver

在我们的旧代码中,我们使用了全局的mgo驱动,类似下面的代码:

  1. // apps 是一个结构体
  2. apps := []model.App{}
  3. err = mgo.Collection.Find(query).Skip(skipCount).Limit(MaxResults).Sort("-starttime").All(&apps)

所以使用新的mongo-driver时,我尝试了下面的代码,使用了Find,但是没有成功。

  1. // 设置FindOneOptions
  2. findOpt := options.Find()
  3. findOpt.SetSkip(int64(skipCount))
  4. limitVal := appsbody.MaxResults
  5. findOpt.SetLimit(int64(limitVal))
  6. findOpt.SetSort("-starttime")
  7. err = mgo.Collection.Find(query, findOpt).All(context.TODO(), &apps)

在上面的代码片段中,参数query的类型是map[string]interface{}
当我尝试记录查询时,Key = type Value = dbuser,它们都是字符串类型。
查询值最初是通过使用query := url.Values{}传递的,这种情况下查询类型将是map[string][]string

我认为后来传递的是map[string]interface{},不确定是否导致了这个问题,无法正确与查询参数params的格式融合,所以我尝试使用下面的代码进行转换,但是这并没有帮助我解决问题。

  1. // 对原始查询进行类型转换
  2. q := make(map[string]interface{}, len(query))
  3. for key, value := range query {
  4. q[key] = value
  5. }

当我尝试运行代码时,它无法执行Find操作,我得到了以下错误并抛出了空指针。

  1. cannot transform type string to a BSON Document: WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel
  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. panic: runtime error: invalid memory address or nil pointer dereference
  3. panic: runtime error: invalid memory address or nil pointer dereference
  4. [signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x564171020634]
  5. goroutine 1 [running]:
  6. go.mongodb.org/mongo-driver/mongo.(*Cursor).closeImplicitSession(0x5641708ab4e0?)
  7. go.mongodb.org/mongo-driver@v1.10.3/mongo/cursor.go:309 +0x14
  8. panic({0x5641716c9440, 0x56417200c2d0})
  9. runtime/panic.go:884 +0x212
  10. go.mongodb.org/mongo-driver/mongo.(*Cursor).Close(0xa1?, {0x5641718f9c30?, 0xc00003a078?})
  11. go.mongodb.org/mongo-driver@v1.10.3/mongo/cursor.go:222 +0x5f
  12. panic({0x5641716c9440, 0x56417200c2d0})
  13. runtime/panic.go:884 +0x212
  14. go.mongodb.org/mongo-driver/mongo.(*Cursor).All(0x0, {0x5641718f9c30, 0xc00003a078}, {0x5641715fa1c0?, 0xc0001d6480?})
  15. go.mongodb.org/mongo-driver@v1.10.3/mongo/cursor.go:251 +0x1ff
  16. cr/dev/usvcs/apps/appDBAccess.DbService.GetApps({{0x5641718fda98, 0xc000484960}, {0x5641718f7228, 0xc000012750}, {0x5641718fce68, 0xc000014030}, {0x564171901cb8, 0x5641720bc5d8}}, 0xc0001dbbf0, {0x0, ...})

不确定我在这里犯了什么错误,有人可以帮助我吗?

英文:

I'm new to mongoDB , Currently we are trying to migrate our old mgo driver to go mongo-driver

In our old code we use something like below from global sign mgo driver

  1. //where apps is a struct
  2. apps := []model.App{}
  3. err = mgo.Collection.Find(query).Skip(skipCount).Limit(MaxResults).Sort("-starttime").All(&apps)

so with the new mongo-driver I tried something like below using Find but that did not work .

  1. // Set FindOneOptions
  2. findOpt := options.Find()
  3. findOpt.SetSkip(int64(skipCount))
  4. limitVal := appsbody.MaxResults
  5. findOpt.SetLimit(int64(limitVal))
  6. findOpt.SetSort("-starttime")
  7. err = mgo.Collection.Find(query, findOpt).All(context.TODO(), &apps)

In the Above snippet params query is of type map[string]interface{}.
when I tried to log the query Key = type Value = dbuser both are of type string
The query value is originally passed by using query := url.Values{}, this case the query type will be map[string][]string

I think later that is passed as map[string]interface{} not sure if that is causing this problem & not able to blend with the right format for query params , So I even tried to converting that using below code , still that did not help me resolve the problem .

  1. //do a type conversion for the original query
  2. q := make(map[string]interface{}, len(query))
  3. for key, value := range query {
  4. q[key] = value
  5. }

when I try to run the code it fails to do Find operation & I get the below err & throws nil pointer

  1. cannot transform type string to a BSON Document: WriteString can only write while positioned on a Element or Value but is positioned on a TopLevel
  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. panic: runtime error: invalid memory address or nil pointer dereference
  3. panic: runtime error: invalid memory address or nil pointer dereference
  4. [signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x564171020634]
  5. goroutine 1 [running]:
  6. go.mongodb.org/mongo-driver/mongo.(*Cursor).closeImplicitSession(0x5641708ab4e0?)
  7. go.mongodb.org/mongo-driver@v1.10.3/mongo/cursor.go:309 +0x14
  8. panic({0x5641716c9440, 0x56417200c2d0})
  9. runtime/panic.go:884 +0x212
  10. go.mongodb.org/mongo-driver/mongo.(*Cursor).Close(0xa1?, {0x5641718f9c30?, 0xc00003a078?})
  11. go.mongodb.org/mongo-driver@v1.10.3/mongo/cursor.go:222 +0x5f
  12. panic({0x5641716c9440, 0x56417200c2d0})
  13. runtime/panic.go:884 +0x212
  14. go.mongodb.org/mongo-driver/mongo.(*Cursor).All(0x0, {0x5641718f9c30, 0xc00003a078}, {0x5641715fa1c0?, 0xc0001d6480?})
  15. go.mongodb.org/mongo-driver@v1.10.3/mongo/cursor.go:251 +0x1ff
  16. cr/dev/usvcs/apps/appDBAccess.DbService.GetApps({{0x5641718fda98, 0xc000484960}, {0x5641718f7228, 0xc000012750}, {0x5641718fce68, 0xc000014030}, {0x564171901cb8, 0x5641720bc5d8}}, 0xc0001dbbf0, {0x0, ...})

Not sure what mistake am i doing here , Can anyone help me with this ?

答案1

得分: 1

问题出在排序值上。它必须是一个文档,而不是一个简单的字符串。它可以是一个映射,一个bson.M(它也是一个映射),或者一个bson.D值(或者任何其他可以“很好地”编组为BSON的值,例如结构体)。

如果你只使用一个字段进行排序,最简单的方法是使用bson.M。还要注意,选项上的方法调用可以链接在一起(它们返回接收者):

  1. findOpt := options.Find().
  2. SetSkip(int64(skipCount)).
  3. SetLimit(int64(appsbody.MaxResults)).
  4. SetSort(bson.M{"starttime": -1})

如果你有多个排序键,顺序是有关系的,这种情况下使用bson.D文档(映射是无序的,bson.D是一个有序的键值对列表):

  1. findOpt := options.Find().
  2. SetSkip(int64(skipCount)).
  3. SetLimit(int64(appsbody.MaxResults)).
  4. SetSort(bson.D{{Key:"starttime", Value: -1}, {Key:"other", Value: 1}})
英文:

The problem is with the sort value. It must be a document, not a simple string. It may be a map, a bson.M (it's also a map) or a bson.D value (or any other values that marshals "nicely" into BSON, e.g. a struct).

If you only use a single field to sort by, simplest is a bson.M. Also note that method calls on options can be chained (they return the receiver):

  1. findOpt := options.Find().
  2. SetSkip(int64(skipCount)).
  3. SetLimit(int64(appsbody.MaxResults)).
  4. SetSort(bson.M{"starttime": -1})

If you'd have multiple sort keys, order does matter, in which case use a bson.D document (maps are unordered, bson.D is an ordered list of key-value pairs):

  1. findOpt := options.Find().
  2. SetSkip(int64(skipCount)).
  3. SetLimit(int64(appsbody.MaxResults)).
  4. SetSort(bson.D{{Key:"starttime", Value: -1}, {Key:"other", Value: 1}})

huangapple
  • 本文由 发表于 2022年10月31日 17:50:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/74261475.html
匿名

发表评论

匿名网友

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

确定