我正在尝试为一组对象创建一个模式,但我不确定如何对后端执行此新操作

I am trying to make a schema for an array of objects and am not sure how to do this new to backend

我正在尝试为对象数组创建架构

我只是想确保到目前为止我所做的是正确的。

const CartSchema = mongoose.Schema({
    cart: [{
        colorC: String,
        sizeC: String,
        date: Date,
        title: String,
        transactionID: Number,
        count: Number,
        lang: []
    }]
});

这是数组在 console.log 时的样子。

[{…}]
0:
category: "Mens Fashion"
colorC: null
count: 1
date: "Mon Jul 29 2019 02:08:07 GMT-0400 (Eastern Daylight Time)"
fabric: "100% Cotton"
id: 1
img: "img/product-1.png"
img2: "img/product-1-1.png"
img3: "img/product-1-2.png"
img4: "img/product-1-3.png"
inCart: true
info: " COMME DES GARCONS PLAY BASIC LOGO TEE"
lang: (3) ["en-US", "en", "pt"]
luxury: "All Luxury items are inspected to verify authenticity"
price: 200
size1: "Small"
size2: "Medium"
size3: "Large"
size4: "Extra Large"
sizeC: "Small"
title: "COMME DES GARCONS TEE"
total: 200
transactionID: 1564380487732
__proto__: Object
length: 1
__proto__: Array(0)

如果您正在尝试收集购物车,并且购物车中有很多产品,您可以这样做:

产品架构:

const ProductSchema = mongoose.Schema({
        colorC: String,
        sizeC: String,
        date: Date,
        title: String,
        transactionID: Number,
        count: Number,
        lang: []
});

然后是包含一系列产品的购物车集合

const CartSchema = mongoose.Schema({
        products: [ProductSchema]
});

const Cart = mongoose.model('Cart', CartSchema);