Need help improving my DB schema to allow for a specific query.

23 views
Skip to first unread message

Stefano Pongelli

unread,
Aug 15, 2013, 6:25:59 PM8/15/13
to mongoo...@googlegroups.com
Hi all, I am having some troubles getting into the Mongoose mindset (bear with me, I am a database noob).

I built this DB schema based on my app. model:

    //Mashup has a number of Containers (containerSchema is a sub-doc)
   
//so that the same Component could belong to two different Containers
   
var containerSchema = new Schema({
          pos
: { top: Number, left: Number }
       
, size: { width: Number, height: Number }
       
, component: { type: ObjectId, ref: 'Component' }
   
})

   
var mashupSchema = new Schema({
          name
: String
       
, desc: String
       
, size: { width: Number, height: Number }
       
, active: Boolean
       
, containers: [containerSchema]
   
})

   
//I am using 'mongoose-schema-extend' to inherit from componentSchema (waiting for the new PR)
   
var componentSchema = new Schema({
          name
: String
       
, desc: String
   
}, { collection : 'components', discriminatorKey : '_type' })

   
//now the various components
   
var imageComponentSchema = componentSchema.extend({
          url
: String
   
})

   
var textComponentSchema = componentSchema.extend({
          text
: String
   
})


   
var htmlComponentSchema = componentSchema.extend({
          html
: String
   
})

   
//this particular component needs a page and a selector
   
//(which could live outside it and belong to multiple components)
   
var webComponentSchema = componentSchema.extend({
          page
: { type: ObjectId, ref: 'Page' }
       
, selector: { type: ObjectId, ref: 'Selector' }
   
})

   
var pageSchema = new Schema({
          name
: String
       
, desc: String
       
, url: String
       
, active: { type: Boolean, default: false }
   
})

   
var selectorSchema = new Schema({
          desc
: String
       
, url: String
       
, cssPath: String
   
})

   
///MODELS
   
var Mashup = mongoose.model("Mashup", mashupSchema)
   
var Component = mongoose.model("Component", componentSchema)
   
var ImageComponent = mongoose.model("ImageComponent", imageComponentSchema)
   
var TextComponent = mongoose.model("TextComponent", textComponentSchema)
   
var HtmlComponent = mongoose.model("HtmlComponent", htmlComponentSchema)
   
var WebComponent = mongoose.model("WebComponent", webComponentSchema)
   
var Page = mongoose.model("Page", pageSchema)
   
var Selector = mongoose.model("Selector", selectorSchema)

A typical scenario would be: the user creates a new Mashup, then adds a Container to it, then decides the type of Component to put inside the Container and finally provides the component contents (with Page and Selector or the other stuff). Containers can be moved between Mashups. Components can belong to a number of Containers. Pages can belong to a number of WebComponents, same for Selectors.

I need to be able to ask for "all mashups", "all components", "all components of type X", "all pages", "all  selectors with url containing Y",.. the usual direct queries... this is easy.

Problem is the following situation:
I have a Page id, and I need to know all Selectors that are used with that Page in any WebComponent that is used by an active Mashup. 

I have no idea how to do this with the actual schema. Any suggestion on how to change it? Thank you.
Reply all
Reply to author
Forward
0 new messages