marcello3d / node-mongolian

[project inactive] Mongolian DeadBeef is an awesome Mongo DB driver for node.js
https://groups.google.com/group/node-mongolian
zlib License
349 stars 50 forks source link

Creating new GridFS files, with the same name, is broken #89

Closed ryancole closed 12 years ago

ryancole commented 12 years ago

It looks like Mongolian's GridFS client is not properly saving files to GridFS when they have the same name as a previously saved file. Using the MongoDB mongofiles tool, it's possible to save the exact same file to the same GridFS document collection over and over. Looking at the MongoDB documentation, the only unique identifier on a GridFS collection should be the _id field. It looks as if Mongolian contains some logic that uses an ambiguous identifier for an upsert, or something, that results in the new file being added to not properly be inserted.

Looking at the Mongolian code, it looks like you're removing a file with the same file name but a different object id. Why is this being done? MongoDB allows files with the same file name, and file name should not be used as a record identifier? https://github.com/marcello3d/node-mongolian/blob/master/lib/gridfile.js#L35

For example, I wrote the following basic test application that inserts a file with the same name and then lists the files in the database. You'd expect to be able to execute this app over and over, and each time see one additional file. That's not what I see, though. What happens is, when I run this the first time it works as intended. When I run it a second time, the original file is there, but the new record is mostly undefined, as if it's using the file name somewhere and that's causing issues. Running it any more times does nothing different than the second time - no new third record, fourth record, etc.

var fs = require('fs'),
    util = require('util'),
    mongolian = new (require('mongolian')),
    database = mongolian.db('test'),
    gridfs = database.gridfs();

// create a new gridfs file
var file = gridfs.create({ filename: 'foo' }),
    write_stream = file.writeStream();

// pipe local data into it
fs.createReadStream('app.js').pipe(write_stream);

// save the file
file.save();

// list all files in this collection, now
gridfs.find().forEach(function (document) {

    // print this file out to console
    console.log('file: ' + util.inspect(document) + '\n');

}, function (err) {

    if (err) { console.log('error: ' + err); }

});
ryancole commented 12 years ago

Use this pull request instead. It created an issue of its' own.

https://github.com/marcello3d/node-mongolian/pull/90