- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个情况,我有一个父文档,我想要两种不同类型的嵌入文档:一个作为父文档,另一个作为子文档,带有可选的父文档。例如:
class ParentDoc
include Mongoid::Document
embeds_many :special_docs
embeds_many :special_doc_groupings
end
class SpecialDoc
include Mongoid::Document
embedded_in :parent_doc
belongs_to :special_doc_groupings
end
class SpecialDocGrouping
include Mongoid::Document
embedded_in :parent_doc
has_many :special_docs
end
在此示例中,SpecialDocs 和 SpecialDocGroupings 可以不存在任何关系,也可以选择具有父子关系。
但是,这是一个无效的 Mongoid 关联,因为我们得到这个错误:
Mongoid::Errors::MixedRelations:
Problem: Referencing a(n) SpecialDoc document from the SpecialDocGrouping document via a relational association is not allowed since the SpecialDoc is embedded.
Summary: In order to properly access a(n) SpecialDoc from SpecialDocGrouping the reference would need to go through the root document of SpecialDoc. In a simple case this would require Mongoid to store an extra foreign key for the root, in more complex cases where SpecialDoc is multiple levels deep a key would need to be stored for each parent up the hierarchy.
Resolution: Consider not embedding SpecialDoc, or do the key storage and access in a custom manner in the application code.
除了 Mongoid 不支持这一事实之外,我没有发现我尝试创建的关联类型有任何问题。
我如何自己实现这种类型的关联?
最佳答案
关联无效,因为当您引用嵌入式模型时,Mongoid 不会将父键存储为外键。这意味着如果您有:
Class Parent
embeds_many :children
end
Class Child
embedded_in :parent
end
您不能引用仅存储其外键的子文档,但您需要存储所有父键,直到到达根。在这种情况下,根由第一个父代表示,您需要存储 2 个键。
您可以手动完成此操作,并毫无问题地创建这种类型的关联。
您的情况有点不同(并且更容易),因为您想要在嵌入同一父项的两个模型之间创建关联。这意味着理论上您不需要存储父 key ,因为模型共享相同的根。Mongoid 不处理这种情况,因此您需要手动创建关联规则和方法。
Class Bar
embeds_many :beers
embeds_many :glasses
end
Class Beer
embedded_in :bar
# Manual has_many :glasses association
def parent
self.bar
end
def glasses
parent.glasses.where(:beer_id => self.id)
end
end
Class Glass
embedded_in :bar
# Manual belongs_to :beer association
field :beer_id, type: Moped::BSON::ObjectId
def parent
self.bar
end
def beer
parent.beers.find(self.beer_id)
end
end
(代码未测试)
关于ruby - Mongoid:如何实现嵌入式文档之间的关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16203108/
我不得不跟踪脏东西。它适用于 parent 博士。但是当我更改嵌入或引用的文档时,脏 必须通过文档本身的嵌入/引用来访问。 如何跟踪父文档本身的脏? 最佳答案 我已经整理了 mongoid 扩展来解决
给定一个带有如下扩展的简单嵌入关系: class D include Mongoid::Document embeds_many :es do def m #...
因此,似乎没有任何干净的方法可以通用地允许具有强参数的 Hash 字段。这当然可能是一个强大的参数问题,但我很好奇是否有解决方法。我有一个包含一些字段的模型... field :name, type:
当我尝试查询 Mongoid 条件的结果并仅保留字段不同的文档时,我感到非常沮丧。这样做: Books.all.distinct(:name) ..只返回名称字段,而不是文档。 还使用 uniq此处另
Mongoid 提供了一些 rake 任务,其中之一为数据库中的所有集合创建索引: 耙数据库:创建索引 但是如果我错了请纠正我,创建索引与实际索引所有项目不是不同吗?我怎样才能重新索引我的文档?如果我
我有一个订阅者类,它有 embeds_many 订阅。订阅具有属性状态。我想添加对状态的验证,以便每个订阅者只有一个订阅可以具有“事件”状态。订户可以拥有多个状态为“已购买”或“已过期”的订阅。 最佳
Mongoid 提供方法 create 和 create!比如 Artist.create(name: "Pablo Picasso") 或 Artist.create!(name: "Pablo P
在 Mongoid 2.x 中,可以执行 Mongoid.database.connection.close 来重置数据库连接。这个特定的 API 在 Mongoid3 中不再可用,重置连接的新方法是
我有一个名为“艺术家”的集合,我想将其重命名为“artist_lookups”。我该怎么做? 最佳答案 使用 mongoid5/mongo ruby 驱动程序 2: # if you need t
目前我为我的类(class)设置了 default_scope,但我希望 rails_admin 使用 .unscoped 执行列表查询 有什么办法可以做到这一点吗?我没有看到覆盖 rails_adm
我知道可以通过数据库调用找到它,但出于好奇,例如在 Node 中,如果我有一个 Mongoose 文档 ID 数组。我如何针对该数组模拟 indexOf 函数以确定其中是否有另一个 mongoId?
Mongoid 没有超时选项。 http://mongoid.org/en/mongoid/docs/installation.html 我希望 Mongoid 终止长时间查询。如何设置 Mongoi
我似乎无法在这里或通过Google找到答案,任何帮助都很棒。 建筑物可以正确保存,但是嵌入式文档PriorityArea不会更新... 我想最终让它均匀地为新的优先级区域添加一个新表格,但是需要首先对
根据 github 上 mongoid 的自述文件,我可以做一些奇特的查询,比如Person.select(:first_name, :last_name).where(:title => "Sir"
有谁知道如何索引和搜索embedded documents与 sunpot_mongoid ? 问题已在 sunspot_mongoid issues 中提出,但至今无解。 最佳答案 刚试过。这是一个
我有一个来自 Devise 的模型用户具有这种关系: 用户名 # Relationships references_many :houses, :dependent => :delete 现在我有一
我在控制台上执行此查询,但是我不能简单地复制并粘贴它以在 mongo shell 中执行它。 有什么方法可以将mongoid DSL转换成真正的mongo查询语句。 谢谢 database=test
在文档中它说你可以使用 inverse_of: nil 但并没有真正描述用例: http://mongoid.org/en/mongoid/docs/relations.html#has_and_be
我正在将 mongoid-history gem 添加到我的项目中。 根据指南in github ,当我将 Userstamp 添加到我的跟踪器时,它会创建 created_by 字段,并使用名为 c
Mongoid.master.collection("seq").find_and_modify({ :query => {:_id => self.class.name}, :up
我是一名优秀的程序员,十分优秀!