Ankur
Ankur

Reputation: 51110

MongoDB returns ObjectId values that are different to what is stored (using GridFS)

I am storing files in MongoDB. In order to retrieve them at a later date I am recording the ObjectId associated with each file.

GridFS fs = new GridFS(Dao.getDB(), "docs");
GridFSInputFile file = fs.createFile(fAsIS);
ObjectId id = (ObjectId)file.get("_id");
file = fs.createFile(fAsIS);  //fAsIS is an InputStream
file.save();

However the value of id that is returned is always different to what is being stored.

E.g. when:

4e9845d6036400df09609b23 is stored the value returned is 4e9845d6036400df09609b22 4e98466f036400df09609b27 is stored the value returned is 4e98466f036400df09609b26 4e9848530364904b6b575003 is stored the value returned is 4e9848530364904b6b575002

The difference is only in the last digit and the difference is consistently the same: the returned value is 1 less than the stored value.

When I run my test with the stored value, the file is returned as expected. So this seems to be the issue I have to solve.

Does anyone know why this might be happening?

Upvotes: 0

Views: 932

Answers (1)

Nat
Nat

Reputation: 3717

Probably because you call fs.createFile(fAsIs) twice

Upvotes: 3

Related Questions