登录
首页 >  Golang >  Go问答

为什么在终端模式下找不到IDE插入的数据?

来源:stackoverflow

时间:2024-02-27 22:00:25 119浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《为什么在终端模式下找不到IDE插入的数据?》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我试图通过 goland ide 在 mongodb 中插入数据。尽管连接正确并且在 ide 输出中我获得了 objectid,但我仍然无法直接从终端看到结果。看来数据库记录了一个新文档,没有任何信息...

osx、mongodb 处于默认设置。驱动程序是“go.mongodb.org/mongo-driver”并且连接正确。 goland时间为2019.2.2

// go

type student struct {
    name string
    sex string
}

newstu := student{
    name: "alice",
    sex: "female",
}

collection := client.database("mgo_1").collection("student")

insertresult, err := collection.insertone(context.todo(), newstu)

if err != nil {
   log.fatal(err)
    }

fmt.println(insertresult.insertedid)

这是插入部分,我按照 mongodb.com 上的指南进行操作

> db.student.find()
{ "_id" : ObjectId("5d82d826f5e2f29823900275"), "name" : "Michael", "sex" : "Male" }
{ "_id" : ObjectId("5d82d845b8db68b150894f5a") }
{ "_id" : ObjectId("5d82dc2952c638d0970e9356") }
{ "_id" : ObjectId("5d82dcde8cf407b2fb5649e7") }

这是我在另一个终端查询的结果。除了第一个有一些内容外,其他三个都是我通过goland尝试插入数据库三次的内容。


解决方案


所以你的结构看起来像这样:

type student struct {
    name string
    sex string
}

namesex 字段不以大写字母开头,因此它们不会导出,因此对反射不可见。 insertone 无疑使用反射来找出 newstu 中的内容,但 student 结构没有导出字段,因此 insertone 根本看不到 newstu 中的任何字段。

如果您将结构修复为具有导出字段:

type Student struct {
    Name string
    Sex string
}

然后 insertone 将能够找出其中的内容。 mongodb 接口应该自行找出从 name (go) 到 name (mongodb) 以及 sexsex 的映射。

本篇关于《为什么在终端模式下找不到IDE插入的数据?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

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