Skip to content


Subversion checkout URL

You can clone with
Download ZIP


Support using `null` as ObjectId for GridStore #933

wants to merge 1 commit into from

2 participants


Regarding the GridStore constructor:

When null is supplied as ObjectId and an extra filename is passed, the GridStore will not try to query by filename.

This patch fixes this.


are you trying to save a file using a filename then you just do

new GridStore(db, "filename", "w")


new GridStore(db, null, "filename", "w")


I got that now, but that wasn't obvious for me by looking at the code and/or the method signature: I thought passing an explicit null-value for the objectId would trigger a filename-based lookup. And besides: the typos in the code make it even more 'unclear'.


yeah it's the hubris of legacy code. the second option is for setting a filename as metadata not as the id itself. The best options is usually to pass in a id and a filename.


The reason i came with this issue is this issue in gridfs-stream:


A 12-lettered (or 24 lettered) string/filename COULD internally be casted to an ObjectId (but this is not the case for GridStore). To prevent this possible mixup, i think it is a good idea to make it a bit more explicit whether a 'filename' or 'ObjectId' is passed


yeah I know but I can't break backward compatibility and this pull request would as it would mange the last options object as you pointed out by modifying a test to make it pass.


That test was broken before i changed anything ;-) I think the change IS backwards compatible!


(i should not have added the test-change in the same pull request, sry)


passes here an on travis without the changes so I'm not sure why it does not pass on you system


Reverted the (unrelated) test change


Typo: indexOf() never returns null


Closing as not going to support

@christkv christkv closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 8, 2013
  1. @Reggino
This page is out of date. Refresh to see the latest.
12 lib/mongodb/gridfs/gridstore.js
@@ -40,7 +40,7 @@ var REFERENCE_BY_FILENAME = 0,
* @class Represents the GridStore.
* @param {Db} db A database instance to interact with.
- * @param {Any} [id] optional unique id for this file
+ * @param {ObjectID} [id] optional unique id for this file
* @param {String} [filename] optional filename for this file, no unique constrain on the field
* @param {String} mode set the mode for this file.
* @param {Object} options optional properties to specify.
@@ -76,14 +76,12 @@ var GridStore = function GridStore(db, id, filename, mode, options) {
this.referenceBy = REFERENCE_BY_ID;
this.fileId = id;
this.filename = filename;
- } else if(typeof filename == 'undefined') {
+ } else if (typeof filename == 'undefined') {
this.referenceBy = REFERENCE_BY_FILENAME;
this.filename = id;
- if (mode.indexOf('w') != null) {
- this.fileId = new ObjectID();
- }
+ this.fileId = new ObjectID();
} else {
- this.referenceBy = REFERENCE_BY_ID;
+ this.referenceBy = ((id === null) ? REFERENCE_BY_FILENAME : REFERENCE_BY_ID);
this.fileId = id;
this.filename = filename;
@@ -170,7 +168,7 @@ var _open = function(self, callback) {
// Create the query
var query = self.referenceBy == REFERENCE_BY_ID ? {_id:self.fileId} : {filename:self.filename};
- query = null == self.fileId && this.filename == null ? null : query;
+ query = null == self.fileId && self.filename == null ? null : query;
// Fetch the chunks
if(query != null) {
12 test/tests/functional/gridstore/gridstore_tests.js
@@ -10,21 +10,25 @@ exports.shouldCreateNewGridStoreObject = function(configuration, test) {
, ObjectID = configuration.getMongoPackage().ObjectID;
var db = configuration.db();
- var gs1
- , gs2
+ var gs
, id = new ObjectID()
, filename = 'test_create_gridstore';
- var gs = new GridStore(db, id, filename, "w");
+ gs = new GridStore(db, id, filename, "w");
test.ok(gs instanceof GridStore);
test.equal(id, gs.fileId);
test.equal(filename, gs.filename);
- var gs = GridStore(db, id, filename, "w");
+ gs = GridStore(db, id, filename, "w");
test.ok(gs instanceof GridStore);
test.equal(id, gs.fileId);
test.equal(filename, gs.filename);
+ gs = GridStore(db, null, filename, "r");
+ test.ok(gs instanceof GridStore);
+ test.equal(filename, gs.filename);
+ test.equal(gs.referenceBy, 0); // 0 = REFERENCE_BY_FILENAME
Something went wrong with that request. Please try again.