A more minimal example
var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
var fs = require('fs');
var conn = mongoose.createConnection('localhost', 'test', 27017 );
conn.once('open', function() {
var gfs = Grid( conn.db, mongoose.mongo );
var source = fs.createReadStream('file.txt');
var target = gfs.createWriteStream({
filename: 'file.txt'
});
source.pipe(target);
});
This puts into the collection:
> db.fs.files.find()
{
"_id" : ObjectId("531470dc8b79087430b2e38d"),
"filename" : "file.txt",
"contentType" : "binary/octet-stream",
"length" : 138,
"chunkSize" : 262144,
"uploadDate" : ISODate("2014-03-03T12:09:00.896Z"),
"aliases" : null,
"metadata" : null,
"md5" : "87be58f21745384bcd13743bde9ae8fb"
}
So your problem was:
- When you first posted you had a stream but you were not using it.
- You thought that was where you told it what file to use
In all the gridFS methods the filename is just a label that is sent to the server. Other methods are used to work with real files. So if you are still confused, try the code then change this line afterwards:
var target = gfs.createWriteStream({
filename: 'dont-exist.txt'
});
Your same file content will be used, just with a new label on the server.