I have added a file into the collection my_collections
.
I am not able check if the file was saved.
With the default bucket (fs.chunk
and fs.files
) the code below works properly
db.my_collection.files.find()
{ "_id" : ObjectId("50e03d29edfdc00d34000001"), "filename" : "hd.txt", "contentType" : "plain/text", "length" : 85, "chunkSize" : 1024, "uploadDate" : ISODate("2014-10-29T06:57:42.375Z"), "aliases" : null, "metadata" : { }, "md5" : "dc20cdc19005d04b8c36a889128170a8" }
Code:
var conn = mongoose.createConnection('localhost', 'testgridfs');
conn.once('open', function () {
var gfs = Grid(conn.db, mongoose.mongo);
var options = {
_id: '50e03d29edfdc00d34000001', // a MongoDb ObjectId
filename: 'hd.txt', // a filename
mode: 'w',
chunkSize: 1024,
content_type: 'plain/text',
root: 'my_collection',//<<<<< **if i comment it ,default collection is taken and codee works fine**
metadata: {}
}
// write (this works in both default and custom bucket case)
// var writestream = gfs.createWriteStream(options);
// fs.createReadStream('/home/pop/Downloads/hd.txt').pipe(writestream);
//chk exits or not
gfs.exist(options , function (err, found) {
if (err) return handleError(err);
found ? console.log('File exists') : console.log('File does not exist');
});
})
The file is shown in db.my_collection.files
but code shows File does not exist
The same code works if the root option is removed.
I want to use multiple buckets as documented in the GridFS reference.
Any suggestions?