KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm having trouble working out how to insert multiple-depths of nested schema in MongoDB, via Mongoose and node.js. The example below is a bit contrived but should hopefully explain my problem. As for why each schema is defined as a full model but not used in the example, that's just because in my real-world problem, they are actual, usable models and I wanted this example to be realistic in case it's relevant. So here are the example schema definitions in reverse order, ie. smallest Russian-doll first: // 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); Hopefully you can tell from this that there is a main model, StationeryBox, which has a label and contains two compartments for DrawingTools and MeasuringTools which are nested schema. They in turn have their own labels and contain nested schemas for Pens and Rulers. The problem I'm having is inserted the 2nd level nesting, ie. pens/rulers. So based on the mongoose docs, creating the top level model, and p
Tags (comma-separated)
Save Edits
Cancel