6
我正在使用mongoose和gridfs-stream來存儲和讀取mongodb中的文件。我在這裏的例子如下:https://github.com/aheckmann/gridfs-stream用node.js,mongoose,gridfs-stream讀取文件
寫入文件到數據庫工作正常,但我面臨讀取文件的問題。
什麼MongoDB的外觀(顯示集合)
fs.chunks
fs.files
什麼文件索引外觀(db.fs.files.find())
{ "_id" : ObjectId("5140392659851df70b000001"),
"filename" : "cover",
"contentType" : "binary/octet-stream",
"length" : 85734,
"chunkSize" : 262144,
"uploadDate" : ISODate("2013-03-13T08:30:30.299Z"),
"aliases" : null,
"metadata" : null,
"md5" : "4476b26067daa0677978ba501308a35d" }
然後我用這個代碼來獲取文件命名爲 「蓋」
...
var gfs = Grid(mongoose.connection.db, mongoose.mongo)
var readstream = gfs.createReadStream('cover')
時出現錯誤:
Error: cover does not exist
at self.collection.self.fileId (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/gridfs/gridstore.js:198:26)
at Cursor.nextObject (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:654:35)
at Cursor.close (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:960:5)
at Cursor.nextObject (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:654:17)
at Cursor.nextObject.commandHandler (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:631:14)
at Db._executeQueryCommand (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:1702:5)
at g (events.js:185:14)
at EventEmitter.emit (events.js:115:20)
at Server.Base._callHandler (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/base.js:130:25)
at Server.connect.connectionPool.on.server._serverState (/mypath/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:517:20)
我用Google搜索了一下,發現了一些可能的相關鏈接:
https://github.com/mongodb/node-mongodb-native/issues/621
Why gridfs get isn't working on file id (ObjectId) only by filename
,如果我取代 「文件名」 與 「5140392659851df70b000001」,它的工作原理:'VAR readstream = gfs.createReadStream( '5140392659851df70b000001')'。我猜這個原因與設置密鑰有關? – LKS 2013-03-13 09:57:46
你也可以看看驅動程序流的實現http://mongodb.github.com/node-mongodb-native/api-generated/gridstore.html#stream – christkv 2013-03-15 07:57:24
只是好奇,你是否存儲對文件的引用一個貓鼬創建的文件?我正在研究使用GridFS,並想知道人們如何實現它。 – Leonidas 2013-03-20 03:36:59