Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:35:02+00:00 2026-05-27T01:35:02+00:00

I’m having trouble working out how to insert multiple-depths of nested schema in MongoDB,

  • 0

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 pushing in the first nested objects works fine, then trouble strikes. 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 the 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 isn’t a super-realistic example, but it’s a simplified example of a real-world system I’m building and this was a easiest way to illustrate that.

The actual saving happens after this of course and I’ve left that out, but do I need to add these next levels in the save callback perhaps?

If anyone can tell me where I’m going wrong on this or point me in the right direction I’ll buy you a nice cake (imaginary cake only I’m afraid, unless you live near me).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:35:04+00:00Added an answer on May 27, 2026 at 1:35 am

    You are extremely close, the problem is actually 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 do have this correct 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 accounts for all of your unexpected behaviour later on.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.