本文介绍了如何填充嵌套的Mongoose嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读并重新阅读了有关Mongoose中嵌入和链接文档的几篇文章.根据我的阅读,我得出的结论是,最好具有类似于以下内容的架构结构:

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

在CategoriesSchema,ColorsSchema和MaterialsSchema中定义的数据将不会经常更改(如果有的话).我认为最好将所有数据都包含在目录模型中,因为尽管有多种类别,颜色和材料,但不会有很多,而且我不需要独立于目录就可以找到它们.

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?

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

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

推荐答案

嵌入式文档数组确实具有push方法.最初创建item

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

就是这样!而且您有嵌入式DocumentArrays.

That's it! And you have Embedded DocumentArrays.

关于代码的其他一些注释,您的目录模型中有两次pattern.为了访问您的其他模型类型,您还需要导出以下类型:

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

这篇关于如何填充嵌套的Mongoose嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:59