Mongoid :: Versioning-如何检查以前的版本? [英] Mongoid::Versioning - how to check previous versions?

查看:122
本文介绍了Mongoid :: Versioning-如何检查以前的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于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?

推荐答案

Mongoid :: Versioning模块向文档添加了一个名为Integer类型的版本的字段,该字段记录了从1开始的当前文档的版本.最大化(如果已定义).此外,您还将创建一个嵌入式文档版本".然后有一个before_save回调函数,它为您处理版本控制.

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:

#Make an empty post, just a title, version 1
post = Post.create(:title => "Hello World")
# Now add some "content" and save, version 2
post.content = "Woo - content"
post.save

这将为我们提供一个类似这样的文档:

That will give us a document something like this:

{
  "title": "Hello World",
  "content": "Woo - content",
  "comments": [
  ]
  "version": 2
  "versions": [
    { "title": "Hello World", "version": 1 }
  ]
}

现在,您只需要使用标准的查找机制即可:

Now you just need to use your standard find mechanisms to get to it:

post = Post.find(:first, :conditions => {:title => "Hello World"})

从中获取最新版本,然后可以以编程方式搜索以前的版本.我会发布输出,但目前没有设置示例.

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.

类似地,如果您希望通过shell进行操作,则只需要基于标题和版本字段运行db.namespace.find().

Similarly you need only run db.namespace.find() based on the title, version fields if you wish to do it via the shell.

希望这是有道理的.

这篇关于Mongoid :: Versioning-如何检查以前的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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