Mongodb schema design for polymorphic objects

I am new to MongoDB and trying to create a simple schema for a collection of python objects. I find it difficult to work with the concept of polymorphism.

Below is some pseudo code. How do you represent this inheritance hierarchy in the 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 am facing is that the A.data schema is dependent on A.content. How can one represent A in a mongodb scheme?

+4
source share
2 answers

Your documents may look like this:

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

So basically β€œdata” refers to an embedded document with document type fields.

+3
source

This does not answer your question, but you can check Ming. It performs polymorphism for you when it maps a document to an object.

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

+1
source

Source: https://habr.com/ru/post/1411604/


All Articles