Mongoid::版本控制 - 如何检查以前的版本?

发布于 2024-12-29 13:48:01 字数 155 浏览 1 评论 0原文

我在基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2025-01-05 13:48:01

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

一般来说,我会推荐一个最大值,但这取决于你。至于如何获取它们,您没有提供示例文档,所以让我们以一篇非常简单的文章为例:

#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

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

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

现在您只需要使用标准查找获取它的机制:

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

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

同样,如果您希望通过 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:

#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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文