douzai2562 2018-02-06 14:03
浏览 109
已采纳

如果将bson ObjectId传递给golang中的GridFS OpenId(),我将收到错误“找不到”

I'm trying to read a video file from MongoDB using GridFS in GoLang. This is my code snippet,

    videoIDHex := bson.ObjectIdHex("5966e9ca0531713218127ddd")
    file, err := mongoDatabase.GridFS("collection_files").OpenId(bson.M{"_id": videoIDHex})
    if err != nil {
        log.Println("Error finding the video : ", err)
    }

When I ran it I always got the error,

not found

But, when I tried using find it works fine. I can able to get the document by,

    videoIDHex := bson.ObjectIdHex("5966e9ca0531713218127ddd")
    collection := mongoDatabase.C("collection_files")
    var file interface{}
    collection.Find(bson.M{"_id": videoIDHex}).One(&file)
    log.Println("file : ", file)

So, how can I solve the issue with GridFS OpenId? If it's solved, I can able to put the GridFS file to buffer and can finally stream it.

  • 写回答

1条回答 默认 最新

  • duannian4784 2018-02-06 14:19
    关注

    It's just id:

    .OpenId(videoIDHex)
    

    not

    .OpenId(bson.M{"_id": videoIDHex})
    

    OpenId wraps the parameter into bson.M by itself: https://github.com/go-mgo/mgo/blob/9a2573d4ae52a2bf9f5b7900a50e2f8bcceeb774/gridfs.go#L197

    Also, collection names should follow the naming conventions. GridFS() accepts a prefix as a single parameter, which is used to build 2 collections: files and chunks: https://github.com/go-mgo/mgo/blob/9a2573d4ae52a2bf9f5b7900a50e2f8bcceeb774/gridfs.go#L100

    mongoDatabase.GridFS("collection_files")
    

    search files in collection collection_files.files

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛