如果将 bson ObjectId 传递给 golang 中的 GridFS OpenId(),我会收到错误 "not found"
If bson ObjectId is passed to GridFS OpenId() in golang, i'm getting error "not found"
我正在尝试使用 GoLang 中的 GridFS 从 MongoDB 读取视频文件。这是我的代码片段,
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)
}
当我 运行 它时,我总是得到错误,
not found
但是,当我尝试使用 find 时,它工作正常。我可以通过
获取文档
videoIDHex := bson.ObjectIdHex("5966e9ca0531713218127ddd")
collection := mongoDatabase.C("collection_files")
var file interface{}
collection.Find(bson.M{"_id": videoIDHex}).One(&file)
log.Println("file : ", file)
那么,如何解决 GridFS OpenId 的问题?如果解决了,我可以将 GridFS 文件放入缓冲区并最终可以对其进行流式传输。
只是id
:
.OpenId(videoIDHex)
不是
.OpenId(bson.M{"_id": videoIDHex})
OpenId
将参数自己包装成 bson.M
:
https://github.com/go-mgo/mgo/blob/9a2573d4ae52a2bf9f5b7900a50e2f8bcceeb774/gridfs.go#L197
此外,集合名称应遵循命名约定。 GridFS()
接受前缀作为单个参数,用于构建 2 个集合:文件和块:https://github.com/go-mgo/mgo/blob/9a2573d4ae52a2bf9f5b7900a50e2f8bcceeb774/gridfs.go#L100
mongoDatabase.GridFS("collection_files")
在集合中搜索文件 collection_files.files
我正在尝试使用 GoLang 中的 GridFS 从 MongoDB 读取视频文件。这是我的代码片段,
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)
}
当我 运行 它时,我总是得到错误,
not found
但是,当我尝试使用 find 时,它工作正常。我可以通过
获取文档 videoIDHex := bson.ObjectIdHex("5966e9ca0531713218127ddd")
collection := mongoDatabase.C("collection_files")
var file interface{}
collection.Find(bson.M{"_id": videoIDHex}).One(&file)
log.Println("file : ", file)
那么,如何解决 GridFS OpenId 的问题?如果解决了,我可以将 GridFS 文件放入缓冲区并最终可以对其进行流式传输。
只是id
:
.OpenId(videoIDHex)
不是
.OpenId(bson.M{"_id": videoIDHex})
OpenId
将参数自己包装成 bson.M
:
https://github.com/go-mgo/mgo/blob/9a2573d4ae52a2bf9f5b7900a50e2f8bcceeb774/gridfs.go#L197
此外,集合名称应遵循命名约定。 GridFS()
接受前缀作为单个参数,用于构建 2 个集合:文件和块:https://github.com/go-mgo/mgo/blob/9a2573d4ae52a2bf9f5b7900a50e2f8bcceeb774/gridfs.go#L100
mongoDatabase.GridFS("collection_files")
在集合中搜索文件 collection_files.files