Mongoid::版本控制 - 如何检查以前的版本?
我在基于 Mongoid 的类中包含了 Mongoid::Versioning 模块。检查文档的先前“版本”或版本的最佳方法是什么?我希望能够看到它的历史。这可以通过 rails console
或 MongoDB shell 进行。查看文档历史记录的最简单方法是什么?
I include the Mongoid::Versioning module in my Mongoid based class. What's the best way to check the previous 'version' or incarnation of a document? I want to be able to see its history. This can be either through rails console
or MongoDB shell. What's the most easiest way to view the document history?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mongoid::Versioning 模块向文档添加一个名为 version 的 Integer 类型字段,该字段记录当前文档的版本,从 1 开始,直到最大值(如果已定义)。此外,您还将创建一个嵌入文档“版本”。然后有一个 before_save 回调为您处理版本控制。
一般来说,我会推荐一个最大值,但这取决于你。至于如何获取它们,您没有提供示例文档,所以让我们以一篇非常简单的文章为例:
这将为我们提供一个类似这样的文档:
现在您只需要使用标准查找获取它的机制:
从中获取最新版本,然后您可以以编程方式搜索以前的版本。我会发布输出,但目前没有设置示例。
同样,如果您希望通过 shell 执行此操作,则只需根据标题、版本字段运行 db.namespace.find() 即可。
希望这是有道理的。
The Mongoid::Versioning module adds a field named version of type Integer to the document, that field records the version of the current document, starting at 1, up to the maximum (if defined). In addition you will have an embedded document "versions" that will be created. There is then a before_save callback which takes care of the versioning for you.
Generally I would recommend a maximum, but that is up to you. In terms of how to get at them, well you didn't give an example document, so let's go with a very simple article as an example:
That will give us a document something like this:
Now you just need to use your standard find mechanisms to get to it:
Grab the latest version out of that, and then you can programmatically search for previous versions. I would post output, but I don't have a sample set up at the moment.
Similarly you need only run db.namespace.find() based on the title, version fields if you wish to do it via the shell.
Hopefully that makes sense.