登录
首页 >  Golang >  Go问答

出现问题:在使用 go mongo-driver执行 Find().All() 时遇到麻烦

来源:stackoverflow

时间:2024-02-10 10:42:23 352浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《出现问题:在使用 go mongo-driver执行 Find().All() 时遇到麻烦》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我是 mongodb 新手,目前我们正在尝试将旧的 mgo 驱动程序迁移到 mongo-driver

在我们的旧代码中,我们使用全局符号 mgo 驱动程序中的类似以下内容

//where apps is a struct
apps := []model.app{}
err = mgo.collection.find(query).skip(skipcount).limit(maxresults).sort("-starttime").all(&apps)

因此,对于新的 mongo-driver,我使用 find 尝试了类似下面的操作,但没有成功。

// set findoneoptions
    findopt := options.find()
    findopt.setskip(int64(skipcount))
    limitval := appsbody.maxresults
    findopt.setlimit(int64(limitval))
    findopt.setsort("-starttime")

    err = mgo.collection.find(query, findopt).all(context.todo(), &apps)

在上面的代码片段中,参数查询的类型为 map[string]interface{}

当我尝试记录查询 key = type value = dbuser 时,两者都是字符串类型 查询值最初是使用 query := url.values{} 传递的,这种情况下查询类型将为 map[string][]string

我认为稍后它会作为 map[string]interface{} 传递,不确定这是否会导致此问题并且无法与查询 params 的正确格式混合,所以我什至尝试使用下面的代码进行转换,但这仍然没有帮助我解决问题。

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

当我尝试运行代码时,它无法执行查找操作,并且出现以下错误并抛出 nil 指针

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
panic: runtime error: invalid memory address or nil pointer dereference
        panic: runtime error: invalid memory address or nil pointer dereference
        panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x40 pc=0x564171020634]

goroutine 1 [running]:
go.mongodb.org/mongo-driver/mongo.(*Cursor).closeImplicitSession(0x5641708ab4e0?)
        go.mongodb.org/[email protected]/mongo/cursor.go:309 +0x14
panic({0x5641716c9440, 0x56417200c2d0})
        runtime/panic.go:884 +0x212
go.mongodb.org/mongo-driver/mongo.(*Cursor).Close(0xa1?, {0x5641718f9c30?, 0xc00003a078?})
        go.mongodb.org/[email protected]/mongo/cursor.go:222 +0x5f
panic({0x5641716c9440, 0x56417200c2d0})
        runtime/panic.go:884 +0x212
go.mongodb.org/mongo-driver/mongo.(*Cursor).All(0x0, {0x5641718f9c30, 0xc00003a078}, {0x5641715fa1c0?, 0xc0001d6480?})
        go.mongodb.org/[email protected]/mongo/cursor.go:251 +0x1ff
cr/dev/usvcs/apps/appDBAccess.DbService.GetApps({{0x5641718fda98, 0xc000484960}, {0x5641718f7228, 0xc000012750}, {0x5641718fce68, 0xc000014030}, {0x564171901cb8, 0x5641720bc5d8}}, 0xc0001dbbf0, {0x0, ...})

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


正确答案


问题出在排序值上。它必须是一个文档,而不是简单的string。它可能是一个映射、一个 bson.M(它也是一个映射)或一个 bson.D 值(或“很好”编组到 bson 中的任何其他值,例如结构)。

如果仅使用单个字段进行排序,最简单的是 bson.m。另请注意,选项上的方法调用可以链接(它们返回接收者):

findopt := options.find().
    setskip(int64(skipcount)).
    setlimit(int64(appsbody.maxresults)).
    setsort(bson.m{"starttime": -1})

如果您有多个排序键,则顺序很重要,在这种情况下,请使用 bson.d 文档(映射是无序的,bson.d 是键值对的有序列表):

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

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《出现问题:在使用 go mongo-driver执行 Find().All() 时遇到麻烦》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>