marcello3d/node-mongolian — Issue #89: Creating new GridFS files, with the same name, is broken
Author: ryancole
Created: 2012-03-05 17:33:28+00:00
State: open

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.

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); }

});
```