6

我正在使用 mongoose 和 gridfs-stream 从 mongodb 存储和读取文件。我在这里遵循示例:https ://github.com/aheckmann/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" }

然后我使用此代码获取名为“cover”的文件

...
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)

我用谷歌搜索并找到了一些可能的相关链接:

https://github.com/mongodb/node-mongodb-native/issues/621

为什么gridfs get不能仅通过文件名处理文件ID(ObjectId)

4

2 回答 2

12

GitHub 上的示例代码有点误导;我最初收到了与您相同的错误消息。就我而言,这是因为我试图在写入流完成之前读取文件。我通过在事件处理程序中读取来解决这个问题"close"

var fs = require("fs"),
    mongo = require("mongodb"),
    Grid = require("gridfs-stream"),
    gridfs,
    writeStream,
    readStream,
    buffer = "";

mongo.MongoClient.connect("mongodb://localhost/gridfs_test", function (err, db) {
    "use strict";
    gridfs = Grid(db, mongo);

    // write file
    writeStream = gridfs.createWriteStream({ filename: "test.txt" });
    fs.createReadStream("test.txt").pipe(writeStream);

    // after the write is finished
    writeStream.on("close", function () {
        // read file, buffering data as we go
        readStream = gridfs.createReadStream({ filename: "test.txt" });

        readStream.on("data", function (chunk) {
            buffer += chunk;
        });

        // dump contents to console when complete
        readStream.on("end", function () {
            console.log("contents of file:\n\n", buffer);
        });
    });
});
于 2013-06-15T03:42:28.597 回答
0

什么是封面给文件格式而不是写入或读取

const express = require("express");
const routes = express.Router();
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/gridfs");
var conn = mongoose.connection;
var path = require("path");
var Grid = require("gridfs-stream");
var fs = require("fs");
Grid.mongo = mongoose.mongo;

var datapath = path.join(__dirname, "../public/1.jpg");

conn.once("open",() =>{
    console.log('connections is opened ');
    console.log(conn.db + "wahab this is running");
    var gfs = Grid(conn.db);
    var filestream = gfs.createWriteStream({
      filename: "wahab.jpg"
    });
    fs.createReadStream(datapath).pipe(filestream);
    filestream.on("close",(file) =>{
      console.log(file.filename  + " Write to DB");
    });

 });

 });
于 2018-03-29T21:21:18.050 回答