简体   繁体   English

Nodejs Mongoose将额外数据添加到模型中

[英]Nodejs Mongoose Add extra data to model

here is the product model: 这是产品型号:

const productSchema= new schema({
_id: {
    type: String,
    default: () => {return uniqid().toUpperCase()}
},
name: {
    type: String
},
price: {
    type: Number
},
type: {
    type: String
},
category: {
    type: String
},
sub_category: {
    type: String
},
images: {
    type: Array
},
sizes: {
    type: Array,
    default: ['OS']
},
materials: {
    type: Array
},
description: {
    type: String
},
weight: {
    type: String,
    default: ''
}
});

and here is the orders model: 这是订单模型:

let orderSchema = new schema({
products: [productModel],
});

in the product model, there is no quantity entry, but I need to pass the quantity of each product that is sent to the orders Api without changing product model. 在产品型号中,没有quantity输入,但我需要传递发送到订单Api的每个产品的数量而不更改产品型号。

example of api call: api调用的例子:

{
"products": [{
    "images": [
        "1",
        "2",
        "3"
    ],
    "sizes": [
        "OS"
    ],
    "materials": [
        "Cotton"
    ],
    "weight": "",
    "_id": "3EC65ISJWW6LU8C",
    "name": "Tshirt",
    "price": 10.99,
    "type": "Clothing",
    "category": "Men Tshirts",
    "description": "A Tshirt",
    "quantity": 5
},{
    "images": [
        "upload_7eb7af15fdaf27bff7667ee35ae4a8b0.png",
        "upload_7dea46a64b046f2d71a75612aaba1523.png",
        "upload_13422483a3b7406620b8e16c0d0ed7df.png"
    ],
    "sizes": [
        "OS",
        "Os2"
    ],
    "materials": [
        "Cotton",
        "M2"
    ],
    "weight": "",
    "_id": "3EC65ISJWW6LVLM",
    "name": "T-Shirt",
    "price": 10.99,
    "type": "Clothing",
    "category": "Men Tshirts",
    "description": "A Tshirt",
    "quantity": 5
}]
}

So I added the quantity entry but it's not in the product model. 所以我添加了quantity条目,但它不在产品模型中。

here is how I did it but it's not working: 这是我如何做到但它不起作用:

products: [productModel, {quantity: Number}],

You can use mongoose discriminators for this to extend the base schema 您可以使用mongoose discriminators来扩展基础架构

Go through this doc for further details 阅读此文档以获取更多详细信息

https://mongoosejs.com/docs/api.html#model_Model.discriminator https://mongoosejs.com/docs/api.html#model_Model.discriminator

Here is a sample example as described in the doc 以下是文档中描述的示例示例

function BaseSchema() {
  Schema.apply(this, arguments);

  this.add({
    name: String,
    createdAt: Date
  });
}
util.inherits(BaseSchema, Schema);

var PersonSchema = new BaseSchema();
var BossSchema = new BaseSchema({ department: String });

var Person = mongoose.model('Person', PersonSchema);
var Boss = Person.discriminator('Boss', BossSchema);
new Boss().__t; // "Boss". `__t` is the default `discriminatorKey`

var employeeSchema = new Schema({ boss: ObjectId });
var Employee = Person.discriminator('Employee', employeeSchema, 'staff');
new Employee().__t; // "staff" because of 3rd argument above

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

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