nodejsはgridfsを利用して文書をmonogoに保存します.
最近開発されたwebはnodejsで開発されました.アップロードされたドキュメントの機能mongoリンク先があります.まずnpm installで必要なモジュールmongo中fs.filesの中にフィールドfilesnameがあります.metadataを利用して、ドキュメントの分類として利用します.
var mongoose = require('mongoose');
var conn = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS');
var fs = require('fs');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var gfs = Grid(conn.db);
var path = require('path');
var util = require('util');
var urlencode = require('urlencode');
var mongooseSchema = new mongoose.Schema({
filename: String,
metadata: String,
aliases: String
}, {collection: "fs.files", versionKey: ""});
var getFile = conn.model('getFile', mongooseSchema);
var fileUpload = {};
// metadata mongo filename
fileUpload.findAllFiles = function (httpId, callback) {
getFile.find({metadata: httpId}, {"filename": '1'}, function (err, docs) {
if (err) {
callback(err);
} else {
callback(null, docs);
}
})
};
// mongo
fileUpload.loadToMongo = function (name,httpId,callback) {
var writestream = gfs.createWriteStream({
filename: name,
metadata:httpId
});
fs.createReadStream(files).pipe(writestream);
writestream.on('close', function (file) {
});
};
// filename mongo
fileUpload.readFile = function (dir, name, callback) {
var fs_write_stream = fs.createWriteStream("documents/" + dir + "/" + name);
var readstream = gfs.createReadStream({
filename: name
});
readstream.pipe(fs_write_stream);
fs_write_stream.on('close', function () {
callback(null);
});
};