Golang – MongoDB(mgo)检索已插入的文件(BSON而不是GridFS)

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

Golang - MongoDB (mgo) retrieve inserted file (BSON not GridFS)

问题

我是一个Golang的新手。
我正在尝试检索我插入的PDF文件对象。由于存储的文件大小不超过16 MB,我不使用GridFS。
对象已经被插入(使用load_file函数),我在MongoDB可视化客户端中看到的对象ID是ObjectId("554f98a400afc2dd3cbfb21b")。

不幸的是,磁盘上创建的文件大小为0 kb。
请指导如何正确检索插入的PDF对象。

谢谢

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "gopkg.in/mgo.v2"
  6. )
  7. type Raw struct {
  8. Kind byte
  9. Data []byte
  10. }
  11. type RawDocElem struct {
  12. Name string
  13. Value Raw
  14. }
  15. func check(err error) {
  16. if err != nil {
  17. panic(err.Error())
  18. }
  19. }
  20. func read_file_content(AFileName string) []byte {
  21. file_contents, err := ioutil.ReadFile(AFileName)
  22. check(err)
  23. return file_contents
  24. }
  25. func save_fetched_file(AFileName RawDocElem) {
  26. ioutil.WriteFile("fisier.pdf", AFileName.Value.Data, 0644)
  27. fmt.Println("FileName:", AFileName.Name)
  28. }
  29. func load_file(AFileName string, ADatabaseName string, ACollection string) {
  30. session, err := mgo.Dial("127.0.0.1,127.0.0.1")
  31. if err != nil {
  32. panic(err)
  33. }
  34. defer session.Close()
  35. session.SetMode(mgo.Monotonic, true)
  36. c := session.DB(ADatabaseName).C(ACollection)
  37. the_obj_to_insert := Raw{Kind: 0x00, Data: read_file_content(AFileName)}
  38. err = c.Database.C(ACollection).Insert(&the_obj_to_insert)
  39. check(err)
  40. }
  41. func get_file(ADatabaseName string, ACollection string, The_ID string) RawDocElem {
  42. session, err := mgo.Dial("127.0.0.1,127.0.0.1")
  43. if err != nil {
  44. panic(err)
  45. }
  46. defer session.Close()
  47. session.SetMode(mgo.Monotonic, true)
  48. c := session.DB(ADatabaseName).C(ACollection)
  49. result := RawDocElem{}
  50. err = c.FindId(The_ID).One(&result)
  51. return result
  52. }
  53. func main() {
  54. //f_name := "Shortcuts.pdf"
  55. db_name := "teste"
  56. the_collection := "ColectiaDeFisiere"
  57. //load_file(f_name, db_name, the_collection)
  58. fmt.Sprintf(`ObjectIdHex("%x")`, string("554f98a400afc2dd3cbfb21b"))
  59. save_fetched_file(get_file(db_name, the_collection, fmt.Sprintf(`ObjectIdHex("%x")`, string("554f98a400afc2dd3cbfb21b"))))
  60. }
英文:

I am a newbie in Golang.
I am trying to retrieve a PDF file object I have inserted. I am not using GridFS, as the files that I would be storing are under 16 MB.
The object has been inserted (using load_file function) and the object ID I am seeing with the MongoDB visual client is ObjectId("554f98a400afc2dd3cbfb21b").

Unfortunately the file created on disk is of 0 kb.
Please advise how to correctly retrieve the inserted PDF object.

Thank you

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "gopkg.in/mgo.v2"
  6. )
  7. type Raw struct {
  8. Kind byte
  9. Data []byte
  10. }
  11. type RawDocElem struct {
  12. Name string
  13. Value Raw
  14. }
  15. func check(err error) {
  16. if err != nil {
  17. panic(err.Error())
  18. }
  19. }
  20. func read_file_content(AFileName string) []byte {
  21. file_contents, err := ioutil.ReadFile(AFileName)
  22. check(err)
  23. return file_contents
  24. }
  25. func save_fetched_file(AFileName RawDocElem) {
  26. ioutil.WriteFile("fisier.pdf", AFileName.Value.Data, 0644)
  27. fmt.Println("FileName:", AFileName.Name)
  28. }
  29. func load_file(AFileName string, ADatabaseName string, ACollection string) {
  30. session, err := mgo.Dial("127.0.0.1,127.0.0.1")
  31. if err != nil {
  32. panic(err)
  33. }
  34. defer session.Close()
  35. session.SetMode(mgo.Monotonic, true)
  36. c := session.DB(ADatabaseName).C(ACollection)
  37. the_obj_to_insert := Raw{Kind: 0x00, Data: read_file_content(AFileName)}
  38. err = c.Database.C(ACollection).Insert(&the_obj_to_insert)
  39. check(err)
  40. }
  41. func get_file(ADatabaseName string, ACollection string, The_ID string) RawDocElem {
  42. session, err := mgo.Dial("127.0.0.1,127.0.0.1")
  43. if err != nil {
  44. panic(err)
  45. }
  46. defer session.Close()
  47. session.SetMode(mgo.Monotonic, true)
  48. c := session.DB(ADatabaseName).C(ACollection)
  49. result := RawDocElem{}
  50. err = c.FindId(The_ID).One(&result)
  51. return result
  52. }
  53. func main() {
  54. //f_name := "Shortcuts.pdf"
  55. db_name := "teste"
  56. the_collection := "ColectiaDeFisiere"
  57. //load_file(f_name, db_name, the_collection)
  58. fmt.Sprintf(`ObjectIdHex("%x")`, string("554f98a400afc2dd3cbfb21b"))
  59. save_fetched_file(get_file(db_name, the_collection, fmt.Sprintf(`ObjectIdHex("%x")`, string("554f98a400afc2dd3cbfb21b"))))
  60. }

答案1

得分: 0

我认为你构建对象ID的方式是错误的。此外,你忘记测试FindId调用后的错误。

我建议尝试导入bson包,并使用以下方式构建对象ID:

  1. hexString := "554f98a400afc2dd3cbfb21b" //24位十六进制字符串
  2. objid := bson.ObjectIdHex(hexString)
  3. ...
  4. err = c.FindId(objid).One(&result)
  5. if err == nil {
  6. // 处理result
  7. ...
  8. } else {
  9. // 错误处理
  10. ...
  11. }
英文:

I think your way to build the object ID is wrong. Furthermore, you forgot to test the error following the FindId call.

I would try to import the bson package, and build the object ID using something like:

  1. hexString := "554f98a400afc2dd3cbfb21b" //24 digits hexadecimal string
  2. objid := bson.ObjectIdHex(hexString)
  3. ...
  4. err = c.FindId(objid).One(&result)
  5. if err == nil {
  6. // Do something with result
  7. ...
  8. } else {
  9. // Error
  10. ...
  11. }

huangapple
  • 本文由 发表于 2015年5月11日 02:55:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/30155409.html
匿名

发表评论

匿名网友

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

确定