使用 GridFS 在 NodeJS 服务器中保存 video/images 到 MongoDB
saving video/images to MongoDB in NodeJS server by using GridFS
我正在寻找使用 GridFS
在 MongoDB
中存储 videos/images 的示例。我遇到了 this official site 并遵循了下面的代码片段
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 中存储文件的任何 link。
编辑 1
我尝试了很多让它工作,最后我知道接口 Grid
已从 Mongodb 2.1
中删除,所以它不会工作,但没有用于替换 Grid 的文档.现在我不清楚要使用哪一个,即 GridStore
、GridFSBucket
、等等
这份文件是否具有误导性?
我相信你想要的是:
var mongo = require('mongodb');
事关require('mongodb'}
:
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
来自文档 here。
我正在寻找使用 GridFS
在 MongoDB
中存储 videos/images 的示例。我遇到了 this official site 并遵循了下面的代码片段
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 中存储文件的任何 link。
编辑 1
我尝试了很多让它工作,最后我知道接口 Grid
已从 Mongodb 2.1
中删除,所以它不会工作,但没有用于替换 Grid 的文档.现在我不清楚要使用哪一个,即 GridStore
、GridFSBucket
、等等
这份文件是否具有误导性?
我相信你想要的是:
var mongo = require('mongodb');
事关require('mongodb'}
:
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON,
assert = require('assert');
来自文档 here。