2

ファイルをgridfsに保存する機能があります。リファクタリングの後、どういうわけか散発的に動作しなくなり、2時間以上それをぼんやりと見つめていました。断言しますが、ほぼ元通りです。クローズを追加する前に最初は機能しなかったことを覚えているようですが、それから機能し始めましたが、それは不眠症である可能性があります. 基本的に問題は、db.fs.files コレクションにレコードがなく、チャンクが db.fs.chunks に追加されていることです。

dataは fs.readFile() 経由でディスクからロードされたバッファです

 31    var gs = new mongodb.GridStore(this.db, filename, "w", {
 32        "chunk_size": 1024*4,
 33        metadata: {
 34          hashpath:gridfs_name,
 35          hash:hash,
 36          name: name
 39        }
 40    });
 41    gs.open(function(err,store) {
 42       gs.write(data,function(err,chunk) {
 43          //cb(err,hash,chunk);
 44          //self.close();
 45       });
 46    });
4

1 に答える 1

6

いくつかの解決策があります。writeBuffer、writeFile、または新しい単純なグリッド クラスを使用できます。以下は、バッファインスタンスを使用するという事実に合わせて調整された例です。

// You can use an object id as well as filename now
var gs = new mongodb.GridStore(this.db, filename, "w", {
  "chunk_size": 1024*4,
  metadata: {
    hashpath:gridfs_name,
    hash:hash,
    name: name
  }
});

gs.open(function(err,store) {
  // Write data and automatically close on finished write
  gs.writeBuffer(data, true, function(err,chunk) {
    // Each file has an md5 in the file structure
    cb(err,hash,chunk);
  });
});

一般に、開始するのに最適な場所は、gridfs クラスの幅広い使用プロファイルをカバーするテストです。見る。

https://github.com/christkv/node-mongodb-native/tree/master/test/gridstore

于 2011-08-04T05:23:24.753 に答える