Kiran Suvarna
Kiran Suvarna

Reputation: 271

If bson ObjectId is passed to GridFS OpenId() in golang, i'm getting error "not found"

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.

Upvotes: 0

Views: 392

Answers (1)

Alex Blex
Alex Blex

Reputation: 37048

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

Upvotes: 2

Related Questions