您好,我需要使用 node.js 在 mongodb 中存储一个文件,该文件放置在我的桌面中,我必须将其存储在我的 mongodb 数据库中。我遇到了一个名为 gridfs 的东西,但不知道如何进一步进行。任何帮助都会非常感激
如果您的文件大小超过 16Mb(Mongo 的最大文档大小),并且希望将文件存储在数据库中,则必须使用 gridFS。
这里有一个非常有用的关于使用 gridFS 的原因的总结:http://docs.mongodb.org/manual/faq/developers/#faq-developers-when-to-use-gridfs
就节点中的实现而言(如果使用本机 mongodb 驱动程序):
var mongodb = require('mongodb')
, MongoClient = mongodb.MongoClient
, Grid = mongodb.Grid //use Grid via the native mongodb driver
;
设置连接后,将文件写入 gridFs 时
var grid = new Grid(db, 'fs'); //db being a handle to your database
var buffer = //read the file in to a buffer
//write the buffer out to mongo
grid.put(buffer, {metadata:{category:'text'}, content_type: 'text'}, function(err, fileInfo) {
if(err) {
//handle any errors here
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)