【问题标题】:Category and Subcategory in mongodb using mongoosemongodb中的类别和子类别使用猫鼬
【发布时间】:2021-07-14 23:01:00
【问题描述】:

我正在使用 nodejs 创建一个电子商务网站。理想情况下,产品将具有类别和子类别。但是我创建的内容应该适用于任何类型的电子商务网站,因此可能不需要子类别字段。

在产品架构中,我添加了与子类别文档相关的子类别字段。并且子类文档与类文档相关。

我的问题是,如何在没有子类别的情况下将产品添加到数据库中?

类别架构:

const categorySchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
    }
})

module.exports = mongoose.model('Category', categorySchema);

子类别架构:

const subCategorySchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
    },
    Category: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Category',
        required:true
    },
})

module.exports = mongoose.model('SubCategory', subCategorySchema);

产品架构:

const productSchema = mongoose.Schema({
    name: {
        type: String,
        required: true,
    },
    description: {
        type: String,
        required: true
    },
    price : {
        type: Number,
        default:0
    },
    SubCategory: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'SubCategory',
        required:true
    },
    countInStock: {
        type: Number,
        required: true,
        min: 0,
        max: 255
    },
})


module.exports = mongoose.model('Product', productSchema);

提前谢谢...

【问题讨论】:

    标签: node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    恐怕您的 ProductSchema 中必须有一个必填的 Category 字段和一个非必填的 SubCategory 字段。这样,您将确保您的所有产品至少有一个类别,并允许添加一个子类别:

    const productSchema = mongoose.Schema({
      name: {
          type: String,
          required: true,
      },
      description: {
          type: String,
          required: true
      },
      price : {
          type: Number,
          default:0
      },
      Category: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Category',
        required: true
      },
      SubCategory: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'SubCategory',
          required: false
      },
      countInStock: {
          type: Number,
          required: true,
          min: 0,
          max: 255
      },
    })
    

    如果您想确保类别和子类别的层次结构,您可以随时在您的预保存中添加一些中间件验证:

    productSchema.pre('save', async function (next) {
      if (this.SubCategory) {
        try {
          const check = await SubCategory.findById(this.SubCategory);
          if (!check || JSON.stringify(check.Category) !== JSON.stringify(this.Category)) {
            throw new Error('Check your Category and/or SubCategory');
          }
        } catch (error) {
          throw error;
        }
      }
      next();
    });
    

    【讨论】:

    • 这很有趣,可能真的可以做到。感谢您的回复和您的时间...但我想了解一些事情。 “check.Category”是如何工作的?
    • SubCategory.findById(this.SubCategory) 采用 SubCategory 集合并查找与您在 product 文档中引入的 SubCategory 具有相同 id 的项目。此查询的结果存储在check 中。因此,如果check 为空,则为该特定产品引入的SubCategory id 无效。第二个条件检查Category:只有当product.Category 匹配作为product.SubCategory 的父级的Category 时,两者都将被视为有效。如果在product 中没有引入SubCategory,这一切都不会发生。
    猜你喜欢
    • 2016-07-25
    • 2020-08-04
    • 2020-05-18
    • 2012-05-08
    • 2019-06-18
    • 1970-01-01
    • 2012-10-14
    • 2016-01-07
    • 2015-04-05
    相关资源
    最近更新 更多