Mongoose互相嵌入文件

时间:2021-06-18 11:58:19

I have a mongoDb database with these collections

我有一个包含这些集合的mongoDb数据库

collection: Category
{
 id:
 catnumber:
 nameNL:
 nameFR:
 subcategories:[id:, nameNL:, nameFR]
 products:[id,nameNL,nameFR]
}
Collection: Product
{
 id:
 nameNL:
 nameFR:
 descriptionNL:
 descriptionFR:
 code1:
 code2:
 categories:[id:,nameNl:,nameFR]
 image:
}

as you see I denormalized the fields.Now i can't figure out how to link this with mongoose schema.

如你所见,我对字段进行了非规范化。现在我无法弄清楚如何将它与mongoose模式联系起来。

I have this, but this doesn't seems to be right:

我有这个,但这似乎不对:

var Schema = mongoose.Schema;
var ProductSchema = new Schema({
 _id: Number,
 nameNL: String,
 nameFR:String,
 descriptionNL:String,
 descriptionFR:String,
 code1:String,
 code2:String,   
 categories: [CategorySchema._id,CategorySchema.nameNL,CategorySchema.nameFR]
});

var CategorySchema = new Schema({
 _id:Number,
 catNumber: String,
 nameNL: String,
 nameFR:String,
 subcategories:[CategorySchema.nameNL,CategorySchema.nameFR],
 products:[ProductSchema._id,ProductSchema.nameNL,ProductSchema.nameFR]
});

Is my denormalization wrong?(The application need to fetch categoryname & productname a lot thats why i denormalized it)

我的非规范化是错误的吗?(应用程序需要获取很多类别名称和产品名称,这就是为什么我对其进行非规范化)

1 个解决方案

#1


Your mongoose syntax is a bit off. Try this (below):

你的mongoose语法有点偏。试试这个(下面):

Note that I've suggested removing the _id fields of your root schemas. Mongo will generate an _id field for you automatically, of type ObjectId, which can come in handy. You do have the option of overriding this if you wish, with _id: Number but I would only recommend doing that if you have a good reason for it.

请注意,我建议删除根模式的_id字段。 Mongo将自动生成一个_id字段,类型为ObjectId,可以派上用场。如果你愿意的话,你可以选择覆盖这个,使用_id:Number,但是如果你有充分的理由,我只建议这样做。

var Schema = mongoose.Schema;
var ProductSchema = new Schema({
    nameNL: String,
    nameFR: String,
    descriptionNL: String,
    descriptionFR: String,
    code1: String,
    code2: String,
    categories: [{
        _id: { type: Schema.Types.ObjectId, ref: 'Category' },
        nameNL: String,
        nameFR: String
    }]
});

var CategorySchema = new Schema({
    catNumber: String,
    nameNL: String,
    nameFR: String,
    subcategories: [{
        _id: { type: Schema.Types.ObjectId, ref: 'Category' },
        nameNL: String,
        nameFR: String   
    }],
    products: [{
        _id: { type: Schema.Types.ObjectId, ref: 'Product' },
        nameNL: String,
        nameFR: String
    }]
});

#1


Your mongoose syntax is a bit off. Try this (below):

你的mongoose语法有点偏。试试这个(下面):

Note that I've suggested removing the _id fields of your root schemas. Mongo will generate an _id field for you automatically, of type ObjectId, which can come in handy. You do have the option of overriding this if you wish, with _id: Number but I would only recommend doing that if you have a good reason for it.

请注意,我建议删除根模式的_id字段。 Mongo将自动生成一个_id字段,类型为ObjectId,可以派上用场。如果你愿意的话,你可以选择覆盖这个,使用_id:Number,但是如果你有充分的理由,我只建议这样做。

var Schema = mongoose.Schema;
var ProductSchema = new Schema({
    nameNL: String,
    nameFR: String,
    descriptionNL: String,
    descriptionFR: String,
    code1: String,
    code2: String,
    categories: [{
        _id: { type: Schema.Types.ObjectId, ref: 'Category' },
        nameNL: String,
        nameFR: String
    }]
});

var CategorySchema = new Schema({
    catNumber: String,
    nameNL: String,
    nameFR: String,
    subcategories: [{
        _id: { type: Schema.Types.ObjectId, ref: 'Category' },
        nameNL: String,
        nameFR: String   
    }],
    products: [{
        _id: { type: Schema.Types.ObjectId, ref: 'Product' },
        nameNL: String,
        nameFR: String
    }]
});