【发布时间】:2016-04-11 11:04:05
【问题描述】:
我正在寻找使用GridFS 将视频/图像存储在MongoDB 中的示例示例。我遇到了this official site 并按照下面的代码sn-p
var MongoClient = require('mongodb').MongoClient,
Grid = mongo.Grid;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(err) return console.dir(err);
var grid = new Grid(db, 'fs');
var buffer = new Buffer("Hello world");
grid.put(buffer, {metadata:{category:'text'}, content_type: 'text'}, function(err, fileInfo) {
if(!err) {
console.log("Finished writing file to Mongo");
}
});
});
当我运行代码时出现以下错误
Grid = mongo.Grid;
^
ReferenceError: mongo is not defined
at Object.<anonymous> (D:\Rahul\Nodejs\Fileupload\fileupload\Samples\grid-fs
\mongo.js:3:8)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
at node.js:951:3
请分享有关如何使用 Nodejs 中的 GridFS 在 MongoDB 中存储文件的任何链接。
编辑 1
我尝试了很多让它工作,最后我知道接口Grid 已从Mongodb 2.1 中删除,所以它不会工作,但没有关于替换 Grid 的文档。现在我不清楚要使用哪一个,即GridStore、GridFSBucket 等
此文档是否具有误导性?
【问题讨论】:
-
讨厌说的很明显,
mongo定义了吗?我敢打赌不是。 -
@BanksySan 我不知道这是什么参考资料,我是从网站上拿来的,如果你知道的话,请你发布答案
-
它告诉你第 3 行的变量
mongo是未定义的。不能比这更明确了。 -
谢谢@BanksySan,我只想知道 mongo 变量应该指向哪里,比如 var mongo = require('mongodb');
标签: mongodb gridfs gridfs-stream