Skip to content

Commit

Permalink
NODE-385 GridStore.exist does not take ObjectId for filename
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Mar 16, 2015
1 parent 9782b12 commit 9b36656
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/gridfs/grid_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,12 @@ GridStore.exist = function(db, fileIdObject, rootCollection, options, callback)
? {'filename':fileIdObject}
: {'_id':fileIdObject}; // Attempt to locate file

// We have a specific query
if(fileIdObject != null && typeof fileIdObject == 'object') {
query = fileIdObject;
}

// Check if the entry exists
collection.findOne(query, {readPreference:readPreference}, function(err, item) {
if(err) return callback(err);
callback(null, item == null ? false : true);
Expand Down
34 changes: 34 additions & 0 deletions test/functional/gridfs_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2737,3 +2737,37 @@ exports['should fail when seeking on a write enabled gridstore object'] = {
});
}
}

/**
* @ignore
*/
exports['should correctly handle filename as ObjectId'] = {
metadata: { requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] } },

// The actual test we wish to run
test: function(configuration, test) {
var GridStore = configuration.require.GridStore
, ObjectID = configuration.require.ObjectID;

var db = configuration.newDbInstance(configuration.writeConcernMax(), {poolSize:1});
db.open(function(err, db) {
var id = new ObjectID();
var gridStore = new GridStore(db, id, id, "w");
gridStore.open(function(err, gridStore) {
gridStore.write("hello world!", function(err, gridStore) {
gridStore.close(function(err, result) {

// Check if file exists
GridStore.exist(db, {filename: id}, function(err, r) {
test.equal(true, r);

db.close();
test.done();
});
});
});
});
});
}
}

0 comments on commit 9b36656

Please sign in to comment.