Jus*_*mas 2 mongodb node.js gridfs
我有一个函数将文件保存到gridfs.在某个重构之后它以某种方式停止了零星的工作,我花了两个多小时茫然地盯着它.我发誓它和它一样大致相同.我似乎记得它在我加入之前没有工作,然后它开始工作,但它可能是失眠.基本上问题是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 });
Run Code Online (Sandbox Code Playgroud)
小智 6
有几种解决方案.您可以使用writeBuffer,writeFile或新的简单网格类.Under是您使用缓冲区实例调整的示例.
// 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);
});
});
Run Code Online (Sandbox Code Playgroud)
通常,最好的起点是覆盖gridfs类的广泛使用配置文件的测试.看着.
https://github.com/christkv/node-mongodb-native/tree/master/test/gridstore
| 归档时间: |
|
| 查看次数: |
8399 次 |
| 最近记录: |