aheckmann / gridfs-stream

Easily stream files to and from MongoDB
MIT License
615 stars 120 forks source link

The .files collection always points to the default 'fs' collection #81

Closed lammas closed 2 years ago

lammas commented 9 years ago

The 'root' option has no effect when using gfs.files.find().

I am not sure if this is by design or a bug. For now it helps to call gfs.collection('myCollectionName') before attempting any find queries. If this is by design the documentation should mention this.

PS. Sorry for multiple edits and confusion.

tomnewport commented 8 years ago

I have the same problem - my files are stored in test_files.files and test_files.chunks.

tilleps commented 8 years ago

I had the same issue. The root option does not appear to work for gfs.createReadStream(). I used gfs.collection() as a workaround.

Example:

var Grid = require('gridfs-stream');

var gfs = Grid(conn.db);
gfs.collection('myCollectionName');

var readStream = gfs.createReadStream();

As a side note, looks like gfs.collection() does NOT work for createWriteStream() and needs the root option parameter to be defined.

var writeStream = gfs.createWriteStream({
   "root": "myCollectionName",
});
Aeonrush commented 7 years ago

Hi everyone You can look here #102 maybe it will be useful for you

vijayakumar-psg587 commented 4 years ago

Since the use of createReadStream and createWriteStream is deprecated, new gridfs stream should be created with a GridFSBucket. Here is an example:


const gfs = new mongoose.mongo.GridFSBucket(await mongoose.connection.useDb(MONGO_FILE_DB).db, {
            bucketName: MONGO_FILE_COLLECTION,
            readPreference: 'primary',
            writeConcern: {w: 1, j: true},
        });

Here I have created and used a different DB and bucketName. You can declare this once and use it elsewhere where files are to be stored and retrieved

You can write to gridfs bukcet using the GridFSAPI of mongodb 3.5. GridfsStream adopted the new API and created whole new bunch of interfaces to interact NOte: I used pump module to make sure that pipe streams are handled appropriately for closures

pump(file, gfs.openUploadStream( fileName, {
            chunkSizeBytes: 5724,
            metadata: {
                desc: 'test file uploaded',
                mimetype: mimetype,
                encoding: encoding
            },
        }));

Now, there is no need to use 'root' as in deprecated interfaces