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

  • Home
  • SEARCH
  • 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 7817261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:10:30+00:00 2026-06-02T06:10:30+00:00

I have read and re-read several posts about embedded and linked documents in Mongoose.

  • 0

I have read and re-read several posts about embedded and linked documents in Mongoose. Based upon what I have read, I have concluded that it would be best to have schema structure similar to the following:

var CategoriesSchema = new Schema({
    year            :    {type: Number, index: true},
    make            :    {type: String, index: true},
    model            :    {type: String, index: true},
    body            :    {type: String, index: true}
});

var ColorsSchema = new Schema({
    name            :    String,
    id                :    String,
    surcharge        :    Number
});

var MaterialsSchema = new Schema({
    name                :    {type: String, index: true},
    surcharge            :    String,
    colors                :    [ColorsSchema]
});

var StyleSchema = new Schema({
    name                :    {type: String, index: true},
    surcharge            :    String,
    materials            :    [MaterialsSchema]
});

var CatalogSchema = new Schema({
    name                 :    {type: String, index: true},
    referenceId            :    ObjectId,
    pattern                :    String,
    categories            :    [CategoriesSchema],
    description            :    String,
    specifications        :    String,
    price                :    String,
    cost                :    String,
    pattern                :    String,
    thumbnailPath        :    String,
    primaryImagePath    :    String,
    styles                :    [StyleSchema]
});

mongoose.connect('mongodb://127.0.0.1:27017/sc');
exports.Catalog = mongoose.model('Catalog', CatalogSchema);

The data defined in CategoriesSchema, ColorsSchema and MaterialsSchema won’t change very often, if ever. I decided it would be better to have all the data in the Catalog model because while there are multiple categories, colors and materials, there won’t be that many and I don’t need to find any of them independent of the Catalog.

But I am totally confused about saving data to the model. Here’s where I get stumped:

var item = new Catalog;
item.name = "Seat 1003";
item.pattern = "91003";
item.categories.push({year: 1998, make: 'Toyota', model: 'Camry', body: 'sedan' });
item.styles.push({name: 'regular', surcharge: 10.00, materials(?????)});

item.save(function(err){

});

With an nested embedded schema like this, how to I get data into the materials and colors embedded documents?

the .push() method doesn’t seem to be available for the nested documents.

  • 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-06-02T06:10:32+00:00Added an answer on June 2, 2026 at 6:10 am

    The array of embedded documents does have the push method. Simply add Embedded Documents after initially creating the item:

    var item = new Catalog;
    item.name = "Seat 1003";
    item.pattern = "91003";
    item.categories.push({year: 1998, make: 'Toyota', model: 'Camry', body: 'sedan' });
    
    var color = new Color({name: 'color regular', id: '2asdfasdfad', surcharge: 10.00});
    var material = new Material({name: 'material regular', surcharge: 10.00});
    var style = new Style({name: 'regular', surcharge: 10.00});
    

    then you can push each embedded doc into their parents:

    material.colors.push(color);
    style.materials.push(material);
    item.styles.push(style);
    

    Then you can save the entire object the database as you where already doing:

    item.save(function(err){});
    

    That’s it! And you have Embedded DocumentArrays.

    A few other notes about your code, you have pattern twice in your Catalog model. And in order to access your other model types, you’ll need to also export those:

    exports.Catalog = mongoose.model('Catalog', CatalogSchema);
    exports.Color = mongoose.model('Colors', ColorsSchema);
    exports.Material = mongoose.model('Materials', MaterialsSchema);
    exports.Style = mongoose.model('Style', StyleSchema);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All - I have read several posts here about svn repositories layout best practices
I have read several posts about the configuration manager in VS2010 (or before) but
First, I must say that I have read several post about this at StackOverflow
I have read several articles and several stackoverflow.com posts about expression tree. It is
I have read several posts about WSDL and SOAP but still I'm confusing the
I have a question about the exit button. I have read several posts on
I have read several articles about UITableView, including the official doc and some on
I have read several documentations about building custom ErrorHandler (by inheriting from IErrorHandler). Unfortunately,
I have read in several places that it's possible to share the objects directory
I have a directory with several .html files. Would like to read the list

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.