Noob Question, please help

agolangf · 2018-04-21 07:30:21 · 675 次点击    
这是一个分享于 2018-04-21 07:30:21 的资源,其中的信息可能已经有所发展或是发生改变。

Hi reddit,

I have recently started learning go. I work on nodejs and MongoDb. While working with MongoDb on go, I am stuck and need your help. My MongoDb collection has very unstructred data and cannot be mapped to a struct , I want to query collection and get the result. Mgo requires me to map my result to a struct and I cant do that. I looked at mgo documentation but got nothing. Stackoverflow search is also futile. Thanks in advance.


评论:

danredux:

Can't be mapped to a struct? That's your issue, everything can be mapped to a struct. Try giving it an empty interface and see what it puts in.

Also, we need to see an example of the unstructured data, just remove anything private.

iAmNotThatGuyJeez:

Hi dan, The collection has documents that has irregularities in key names thus mapping them to struct is hard. Although i figured that slice of bson.M works with Find All query. Thx for reply.

danredux:

map[string]interface{} is an OK type, if it helps. It lets arbitrary strings go in as keys. Glad you worked it out, though.

pierrrre:

map it to a bson.M

iAmNotThatGuyJeez:

Hi bson.M only works with find One. Bson. D works with find all but returns nil. I want to query all.

pierrrre:

[]bson.M


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

675 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传