Mongoid :: Versioning - how to check previous versions?

I turn on the Mongoid :: Versioning module in my Mongoid-based class. What is the best way to check a previous version or implementation of a document? I want to see his story. This can be either through the rails console or the MongoDB shell. The easiest way to view document history?

+2
source share
1 answer

The Mongoid :: Versioning module adds to the document a field with a name of type of type Integer, this field writes the version of the current document, starting from 1, to the maximum (if defined). In addition, you will have an embedded “version” document to be created. Then, the before_save callback is executed, which takes care of the version for you.

I would usually recommend the maximum, but that is up to you. As for how to get to them, it’s good that you didn’t submit a sample document, so release as a simple article:

 #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 

This 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 standard search engines to get to it:

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

Retrieve the latest version, and then you can programmatically search for previous versions. I would publish the conclusion, but at the moment I have no sample.

Similarly, you only need to run db.namespace.find () based on the header, the version field, if you want to do this through the shell.

Hope this makes sense.

+7
source

Source: https://habr.com/ru/post/1395066/


All Articles