Mongodb schema design for polymorphic objects

mongodb, python

Solution

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.

Problem

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. 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 = * ``` 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?

Original source