6

I am using mongoose and gridfs-stream to store and read files from mongodb. I am following the example here: https://github.com/aheckmann/gridfs-stream

Writing files into db is working fine but I faced a problem to read files.

What the mongodb looks (show collections)

fs.chunks
fs.files

What the file index looks (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" }

Then I use this code to get file named "cover"

...
var gfs = Grid(mongoose.connection.db, mongoose.mongo)
var readstream = gfs.createReadStream('cover')

An error occured:

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)

I googled it and found some possible related links:

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

Why gridfs get isn't working on file id (ObjectId) only by filename

Community
  • 1
  • 1
LKS
  • 673
  • 2
  • 10
  • 21
  • 1
    if I replace the "filename" with "5140392659851df70b000001", it works: `var readstream = gfs.createReadStream('5140392659851df70b000001')`. I guess the reason is related to setting the key? – LKS Mar 13 '13 at 09:57
  • You can also have a look at the driver stream implementation http://mongodb.github.com/node-mongodb-native/api-generated/gridstore.html#stream – christkv Mar 15 '13 at 07:57
  • 1
    Just curious, are you storing a reference to the file in a mongoose created document? I'm looking into using GridFS and was wondering how folks implement it. – Leonidas Mar 20 '13 at 03:36
  • fs.files stores all the information of file while fs.chunks stores the data of the file. Mongo uses ObjectId("5140392659851df70b000001") in fs.files to look up the data in fs.chunks. So fs.files store the reference to the real data chunks. – LKS Mar 20 '13 at 03:58

2 Answers2

12

The example code on GitHub was a little bit misleading; I initially received the same error message you did. In my case, it was due to the fact that I was attempting to read the file before the write stream had finished. I resolved this by doing the read inside the event handler for "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);
        });
    });
});
Robert Mitchell
  • 1,334
  • 8
  • 10
0

what is cover give an format to file than write or read

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");
    });

 });

 });