查找MongoDB中存储的文档的最新版本-GridFs [英] Find the latest version of a document stored in MongoDB - GridFs

查看:81
本文介绍了查找MongoDB中存储的文档的最新版本-GridFs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我计划使用MongoDB GridF来存储不同类型的平面文件,脚本等.使用GridF的好处之一是,它会在每个插入项上创建一个新文档,因此可用于使用自定义元数据等进行版本控制的目的.

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.

但是,如何确保获取的文档是GridFS的最新文档?

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

赞赏任何建议.

推荐答案

对于任何给定的条件,GridFS始终使用findOne带来最新版本. 你可以测试一下.具有相同文件名"字段的许多文件.然后将文件名传递给findOne,您将获得最新的文件名.

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.

或者,您可以对uploadDate进行排序以获取所有版本.

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

db.files.find({'filename':'abc.pdf'}).sort({'uploadDate':-1})

这篇关于查找MongoDB中存储的文档的最新版本-GridFs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆