0

I'm planning to use MongoDB GridFs to store different types of flat files, scripts etc. One of the benefits using GridFs is that it creates new document on every insert and thus can be used for versioning purposes using custom meta data etc.

However, how to ensure the fetched document is the latest document from GridFS ?

Appreciate any suggestions.

codehammer
  • 876
  • 2
  • 10
  • 27

1 Answers1

1

For any given criteria, GridFS always brings the latest version with findOne. You can test it. Have lots of files with same 'filename' field. Then pass filename to findOne, you will get the latest one.

Alternatively, you can sort on the uploadDate to get all versions.

db.files.find({'filename':'abc.pdf'}).sort({'uploadDate':-1})
ramazan polat
  • 7,111
  • 1
  • 48
  • 76
  • 2
    I can not reproduce your statement that findOne() returns the latest one. From my point of view it returns the oldest one... I'm using spring-data-mongodb 1.5.0.RELEASE... – user1145874 May 29 '14 at 08:49
  • Sorry I am not familiar with spring-data-mongodb but the you can use mongo client REPL to test this. – ramazan polat Aug 12 '15 at 11:09