嵌入式文档与杂项中的哈希数据类型 [英] embedded document vs hash datatype in mongoid

查看:84
本文介绍了嵌入式文档与杂项中的哈希数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何有关此的博客文章或文档.它们两者,嵌入式文档和哈希数据类型,都非常相似.彼此之间的好处或局限性是什么?

考虑我的架构设计:

class HistoryTracker
  include ::Mongoid::Document
  include ::Mongoid::Timestamps

  field :modifier,          type: Hash,    default: {}
  field :original,          type: Hash,    default: {}
  field :modified,          type: Hash,    default: {}
  field :changeset,         type: Hash,    default: {}
end

我应该在HistoryTracker类中创建几个嵌入式文档吗?还是只是用那个?索引如何?

解决方案

Mongoid在数据库级别以几乎相同的方式存储嵌入式文档和Hash属性.在使用mongoid声明模型中的字段时,这是正常的,因此,如果您具有嵌套结构,则可以正常创建嵌入式文档.由于MongoDB是无架构的Mongoid,因此您需要声明字段,以便以与ActiveRecord相同的API形式呈现它们.但是在某些用例中,Hash属性为您提供了更多的灵活性.这种灵活性的缺点是,您只能使用Hash API,因此无法获得自动生成的属性方法,也无法像通常在模型类中那样封装业务逻辑.

作为一个例子,假设您有一个问卷模型,您需要在其中存储包含许多问答对的许多部分.如果系统的关键要求是管理员能够设置新的部分和问题,那么您将不容易将答案建模为包含每个问题的显式字段的常规嵌入式文档.对于这种事情,哈希可能更有意义.

我不知道您的特定要求是什么,但是作为一个粗略的指导,我会说,当您使用带有嵌入式文档的固定架构时,但是当您需要开放式模型时,请考虑Hash属性. /p>

I can't find any blog post or documentation talking about this. They both, embedded document and hash data type, are quite similar. What the benefit or limitation of each over the other?

Consider my schema design:

class HistoryTracker
  include ::Mongoid::Document
  include ::Mongoid::Timestamps

  field :modifier,          type: Hash,    default: {}
  field :original,          type: Hash,    default: {}
  field :modified,          type: Hash,    default: {}
  field :changeset,         type: Hash,    default: {}
end

Should I create several embedded document inside this HistoryTracker class? or just use that? How about indexing?

解决方案

Mongoid stores embedded documents and Hash attributes in pretty much the same way at the database level. Its normal when working with mongoid to declare your fields in your models so if you have a nested structure its normal to create an embedded document. Because MongoDB is schema-less mongoid needs you to declare fields in order to present them in the same kind of API that ActiveRecord does. But for some use cases a Hash attribute gives you a bit more flexibility. The downside of that flexibility is that you are limited to the Hash API so you don't get auto-generated attribute methods and you can't encapsulate business logic in the way that you might normally do within a model class.

As an example, suppose you have a Questionnaire model in which you need to store many sections containing many question-answer pairs. If a key requirement of the system is for the administrator to be able to setup new sections and questions then you would not easily be able model the answers as a regular embedded document containing explicit fields for each question. For that kind of thing a Hash might make more sense.

I don't know what your specific requirements are but as a rough guide I would say that when you are working with a fixed schema stick with an embedded document, but when you need an open-ended model consider Hash attributes.

这篇关于嵌入式文档与杂项中的哈希数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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