Node, Mongoose, problems with preserving multiple depths of a nested schema

I'm having trouble developing how to embed the multiple depths of a nested schema in MongoDB through Mongoose and node.js.

The example below is a little clever, but hopefully explain my problem. As for why each circuit is defined as a complete model, but not used in the example, it is simply because in my real problem they are relevant, useful models, and I wanted this example to be realistic if it matters.

So, here is an example of defining a circuit in reverse order, i.e. smallest russian doll:

// Define pen model var PenSchema = new Schema({ color: String // black, blue or red }); var Pen = mongoose.model('Pen', PenSchema); // Define ruler model var RulerSchema = new Schema({ units: String // inches or millimetres }); var Ruler = mongoose.model('Ruler', RulerSchema); // -------- // Define drawing tools model var DrawingToolsSchema = new Schema({ label: String, pens: [Pen] }); var DrawingTools = mongoose.model('DrawingTools', DrawingToolsSchema); // Define measuring tools model var MeasuringToolsSchema = new Schema({ label: String, ruler: [Ruler] }); var MeasuringTools = mongoose.model('MeasuringTools', MeasuringToolsSchema); // -------- // Define stationery box model // It has a label and two compartments - tools for drawing and measuring var StationeryBoxSchema = new Schema({ label: String, drawingTools: [DrawingToolsSchema], measuringTools: [MeasuringToolsSchema] }); var StationeryBox = mongoose.model('StationeryBox', StationeryBoxSchema); 

I hope you can say from this that there is a main model, StationeryBox, which has a shortcut and contains two compartments for DrawingTools and MeasuringTools, which are a nested circuit. They, in turn, have their own shortcuts and contain nested schemas for Pens and Rulers. The problem I encountered is a nesting of the 2nd level, i.e. pens / rulers. Thus, based on mongoose documents, creating a top-level model, and clicking on the first nested objects works fine, then the problem hits. For example:

 // To create my stationery box - this works var stationery = new StationeryBox({ label: 'My Stationery Box' }); // To add the nested compartments - this works stationery.drawingTools.push({ label: 'My Pens' }); stationery.measuringTools.push({ label: 'My Rulers' }); // But this is wrong as 'stationery.drawingTools.pens' is undefined stationery.drawingTools.pens.push({ color: 'red' }); stationery.drawingTools.pens.push({ color: 'black' }); 

And if I go back one step and try to insert pens at the same time as the drawing tools:

 // Also wrong - presumably the second level of nesting is the problem stationery.drawingTools.push({ label: 'My Pens', pens: [ // These object represent second levels of nested schema { color: 'red' }, { color: 'black' } ] }); 

I know this is not a super-realistic example, but it is a simplified example of a real system that I create, and it was the easiest way to illustrate this.

The actual save happens after that, of course, and I left it, but I need to add these next levels in the save callback, maybe?

If someone tells me where I'm wrong, or call me in the right direction, I will buy you a good cake (an imaginary cake, only I am afraid if you do not live next to me).

+7
source share
1 answer

You are very close, the problem is in your Schema definitions. It all comes down to the difference between a Schema object and a Model object. When specifying mongoose Schema with embedded documents, you can only point to other Schema .

 var DrawingToolsSchema = new Schema({ label: String, pens: [Pen] // uh-oh, broken! Pen is a Model. }); 

However, you have it right for your first level of embedded documents defined in StationeryBoxSchema .

 var StationeryBoxSchema = new Schema({ label: String, drawingTools: [DrawingToolsSchema], // yes! DrawingToolsSchema is a Schema measuringTools: [MeasuringToolsSchema] // this one too. }); 

This difference explains your unexpected behavior later.

+9
source

All Articles