How would you represent an object that can be of multiple types, when storing it as a document in MongoDB?

Posted by blueberryfields on Programmers See other posts from Programmers or by blueberryfields
Published on 2012-03-08T21:19:14Z Indexed on 2012/04/07 23:43 UTC
Read the original article Hit count: 273

Somewhat related to this question, say that I have an object category which, depending on which type of object I have, has different restrictions on what it contains. (if you can reword the previous sentence to make more sense I'd appreciate it)

For example

var SomeSchema = new Schema({
  _id: ObjectID,
  [... large number of common fields ...]
  type: //Restricted somehow to members of a fixed array
  data: //depending on type, this might be restricted - sometimes an integer, sometimes array, etc...
});

What's the idiosyncratic method for defining this type of schema? Is it appropriate to define a single schema, and handle the types inside of it's members, or am I better off with separate schema for each type?

© Programmers or respective owner

Related posts about JavaScript

Related posts about architecture