Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb schema design for polymorphic objects

Tags:

python

mongodb

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?

like image 327
GeneralBecos Avatar asked Oct 08 '22 21:10

GeneralBecos


2 Answers

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.

like image 148
Kyle Banker Avatar answered Oct 10 '22 11:10

Kyle Banker


This doesn't particularly answer your question, but you might check out Ming. It does polymorphism for you when it maps the document to the object.

http://merciless.sourceforge.net/tour.html

like image 30
Eve Freeman Avatar answered Oct 10 '22 11:10

Eve Freeman