我已将文件添加到集合my_collections
中。
我无法检查文件是否已保存。
使用默认存储桶 (fs.chunk
和fs.files
),下面的代码可以正常工作
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" }
代码:
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');
});
})
该文件显示在db.my_collection.files
但代码显示File does not exist
如果删除了 root 选项,则相同的代码可以工作。
我想使用GridFS 参考中记录的多个存储桶。
有什么建议么?