Find the latest version of a document stored in MongoDB - GridFs

折月煮酒 提交于 2019-12-12 10:24:20

问题


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.


回答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})


来源:https://stackoverflow.com/questions/19056800/find-the-latest-version-of-a-document-stored-in-mongodb-gridfs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!