0

我已将文件添加到集合my_collections中。

我无法检查文件是否已保存。

使用默认存储桶 (fs.chunkfs.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 参考中记录的多个存储桶。

有什么建议么?

4

1 回答 1

0

问题出在版本中。github 上的代码是最新的 0.5.3,而在 npm 上是 0.5.1..

所以我已经从 github 手动下载,一切正常。 https://github.com/aheckmann/gridfs-stream

于 2014-10-30T07:26:26.043 回答