文档数据库中的继承? [英] inheritance in document database?
问题描述
我想知道是因为我在 pdf[noSql] 权威指南"和开始 [noSql]"中搜索了继承"这个词,但我什么也没找到?我错过了什么吗?因为我正在使用 hibernate 和 mysql 进行 tablePerHierarchy 继承,所以在 [noSql] 中是否由于某种原因被弃用了?
I am wondering because I searched the pdf "[noSql] the definitive guide" and "beginning [noSql]" for the word "inheritance" but I didn't find anything? am I missing something? because I'm doing a tablePerHierarchy inheritance with hibernate and mysql, does that become deprecated for some reason in [noSql]?
(将[noSql]替换成你喜欢的not only sql"数据库)
(replace [noSql] with the "not only sql" database you like)
推荐答案
我知道这个答案有点晚了,但是对于 MongoDB,您可能会看到一些稍微不同的东西.
I know this answer is a little late, but for MongoDB, you're probably looking at something slightly different.
Mongo 是无模式的,因此tablePerHierarchy"的概念不一定有用.
Mongo is schemaless, so the concept of "tablePerHierarchy" is not necessarily useful.
假设如下
class A
property X
property Y
property Z
class B inherits from A
property W
在 RDMS 中,你可能会有这样的东西
In an RDMS you would probably have something like this
table A: columns X, Y, Z
table B: columns X, Y, Z, W
但是 MongoDB 没有模式.所以你不需要以这种方式构造数据.相反,您将拥有一个包含所有类型 A 或 B(或 C...)的对象(或文档")的集合".
But MongoDB does not have a schema. So you do not need to structure data in this way. Instead, you would have a "collection" containing all objects (or "documents") of type A or B (or C...).
所以你的收藏将是一系列像这样的对象:
So your collection would be a series of objects like this:
{"_id":"1", "X":1, "Y":2, "Z":3}
{"_id":"2", "X":5, "Y":6, "Z":7, "W":6}
您会注意到,我将 A 类型的对象存储在 B 类型的对象旁边.MongoDB 使这变得非常容易.只需从集合中拉出一个文档,它就会神奇地"拥有所有适当的字段/属性.
You'll notice that I'm storing objects of type A right beside objects of type B. MongoDB makes this very easy. Just pull up a Document from the Collection and it "magically" has all of the appropriate fields/properties.
但是,如果您有数据对象"或实体",则可以通过添加类型使您的生活更轻松.
However, if you have "data objects" or "entities", you can make your life easier by adding a type.
{"_id":"1", "type":"A", "X":1, "Y":2, "Z":3}
{"_id":"2", "type":"B", "X":5, "Y":6, "Z":7, "W":6}
这样可以更轻松地编写用于加载对象的工厂类.
This makes it easier to write a factory class for loading your objects.
这篇关于文档数据库中的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!