dongmuyan5638 2015-05-10 18:55
浏览 65
已采纳

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

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

package main

import (
    "fmt"
    "io/ioutil"

    "gopkg.in/mgo.v2"
)

type Raw struct {
    Kind byte
    Data []byte
}

type RawDocElem struct {
    Name  string
    Value Raw
}

func check(err error) {
    if err != nil {
        panic(err.Error())
    }
}

func read_file_content(AFileName string) []byte {

    file_contents, err := ioutil.ReadFile(AFileName)
    check(err)
    return file_contents
}

func save_fetched_file(AFileName RawDocElem) {
    ioutil.WriteFile("fisier.pdf", AFileName.Value.Data, 0644)
    fmt.Println("FileName:", AFileName.Name)
}

func load_file(AFileName string, ADatabaseName string, ACollection string) {
    session, err := mgo.Dial("127.0.0.1,127.0.0.1")

    if err != nil {
        panic(err)
    }
    defer session.Close()

    session.SetMode(mgo.Monotonic, true)

    c := session.DB(ADatabaseName).C(ACollection)

    the_obj_to_insert := Raw{Kind: 0x00, Data: read_file_content(AFileName)}

    err = c.Database.C(ACollection).Insert(&the_obj_to_insert)
    check(err)

}

func get_file(ADatabaseName string, ACollection string, The_ID string) RawDocElem {
    session, err := mgo.Dial("127.0.0.1,127.0.0.1")

    if err != nil {
        panic(err)
    }
    defer session.Close()

    session.SetMode(mgo.Monotonic, true)
    c := session.DB(ADatabaseName).C(ACollection)

    result := RawDocElem{}
    err = c.FindId(The_ID).One(&result)
    return result
}

func main() {
    //f_name := "Shortcuts.pdf"
    db_name := "teste"
    the_collection := "ColectiaDeFisiere"
    //load_file(f_name, db_name, the_collection)
    fmt.Sprintf(`ObjectIdHex("%x")`, string("554f98a400afc2dd3cbfb21b"))
    save_fetched_file(get_file(db_name, the_collection, fmt.Sprintf(`ObjectIdHex("%x")`, string("554f98a400afc2dd3cbfb21b"))))
}
  • 写回答

1条回答 默认 最新

  • duanluangua8850 2015-05-11 11:52
    关注

    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:

    hexString := "554f98a400afc2dd3cbfb21b" //24 digits hexadecimal string
    objid := bson.ObjectIdHex(hexString)
    ...
    err = c.FindId(objid).One(&result)
    if err == nil {
       // Do something with result
       ...
    } else {
       // Error
       ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误