繁体   English   中英

如何填充嵌套的Mongoose嵌入文档

[英]How to populate nested Mongoose embedded documents

我已经阅读并重新阅读了几篇关于Mongoose中嵌入和链接文档的帖子。 根据我所读到的内容,我得出结论,最好使用类似于以下的模式结构:

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);

如果有的话,CategoriesSchema,ColorsSchema和MaterialsSchema中定义的数据不会经常更改。 我决定在Catalog模型中包含所有数据会更好,因为虽然有多个类别,颜色和材料,但不会有那么多,我不需要找到任何独立于Catalog的数据。

但我对将数据保存到模型完全感到困惑。 这是我被难倒的地方:

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){

});

使用这样的嵌套嵌入式架构,如何将数据导入到嵌入文档的材料和颜色中?

.push()方法似乎不适用于嵌套文档。

嵌入文档数组确实有push方法。 只需在最初创建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});

那么你可以将每个嵌入式文档推送到他们的父母:

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

然后,您可以将整个对象保存在数据库中,就像您在哪里一样:

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

而已! 而且你有Embedded DocumentArrays。

关于你的代码的一些其他注意事项,你有pattern在你的目录型号的两倍。 并且为了访问您的其他模型类型,您还需要导出:

exports.Catalog = mongoose.model('Catalog', CatalogSchema);
exports.Color = mongoose.model('Colors', ColorsSchema);
exports.Material = mongoose.model('Materials', MaterialsSchema);
exports.Style = mongoose.model('Style', StyleSchema);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM