多态对象的Mongodb模式设计 [英] Mongodb schema design for polymorphic objects

查看:88
本文介绍了多态对象的Mongodb模式设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB的新手,正在尝试为一组python对象设计一个简单的架构.我在处理多态性的概念上遇到了困难.

I'm new to MongoDB and am trying to design a simple schema for a set of python objects. I'm having a tough time working with the concept of polymorphism.

下面是一些伪代码.您将如何在MongoDB模式中表示此继承层次结构:

Below is some pseudo-code. How would you represent this inheritance hierarchy in MongoDB schema:

class A: 
    content = 'video' or 'image' or 'music'
    data    = contentData  # where content may be video or image or music depending on content.

class videoData:
    length = *
    director = *
    actors = *       

class imageData:
    dimensions = *

class musicData:
    genre = *

我面临的问题是A.data的架构取决于A.content.如何在mongodb模式中表示A?

The problem I'm facing is that the schema of A.data depends on A.content. How can A be represented in a mongodb schema?

推荐答案

您的文档可能如下所示:

Your documents could look like this:

{ _type: "video",
  data: {
    length: 120,
    director: "Smith",
    actors = ["Jones", "Lee"]
  }
}

因此,基本上,数据"指向具有文档类型指定字段的嵌入式文档.

So, basically, "data" points to an embedded document with the document's type-specified fields.

这篇关于多态对象的Mongodb模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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