I read and re-read some posts on embedded and related documents in Mongoose. Based on what I read, I came to the conclusion that it would be better to have a circuit 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);
Data defined in CategoriesSchema, ColorsSchema and MaterialsSchema will not change very often, if ever. I decided that it would be better to have all the data in the catalog model, because when there are several categories, colors and materials, there will not be so many, and I do not need to look for any of them regardless of the Catalog.
But I am completely confused by the fact that I am saving data in the model. Here where I get the dead end:
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 a nested embedded circuit, how is it, how can I get data into materials and colors of embedded documents?
.push(), , .