當我使用grid.put()將文件寫入GridFS時,它具有與之前存儲的文件相同的文件名,第一個文件將被覆蓋。實際上,相同的文件名只能在數據庫中存在一次,或者我做錯了什麼?nodejs gridfs grid.put()具有相同的文件名覆蓋文件
我的代碼如下所示:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
Grid = mongo.Grid;
server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('mydb', server);
db.open(function(err, db) {
var buffer = new Buffer("This is the first sample text");
grid.put(buffer, {metadata:{}, filename: "test.txt", content_type: 'text'}, function(err, fileInfo) {
buffer = new Buffer("This is the second sample text");
// now this overwrites the first one...
grid.put(buffer, {metadata:{}, filename: "test.txt", content_type: 'text'}, function(err, fileInfo) {
});
});
});
我認爲該文件是由._id的ObjectId,而不是由文件名指定的唯一。我錯了嗎?
感謝您的幫助!
謝謝!這是一個有用的答案。 – heinob