更新MEAN中的关联集合

问题描述 投票:0回答:1

我正在构建一个MEAN堆栈Web应用程序,我有两个相关的集合类别和品牌。

现在,当我添加一个新品牌并自动选择其类别时,类别ID保存在品牌架构中,但我不知道当我删除该类别时如何更新(设置为空)此字段(品牌架构中的类别ID)从类别架构...希望我明白我的问题。

const mongoose = require('mongoose');

  const categorySchema = mongoose.Schema({
  categoryname: { type: String, required:true},
  categoryBrands: [{
     type: mongoose.Schema.Types.ObjectId,
     ref: "Brand",
     required: true }]
    });
 module.exports = mongoose.model('Category', categorySchema);

品牌系列架构:

const mongoose = require('mongoose');
const brandSchema = mongoose.Schema({
   brandName: { type: String, required: true },
   brandCategory: {
       type: mongoose.Schema.Types.ObjectId,
       ref: "Category",
       required: true
    }
  });
  module.exports = mongoose.model('Brand', brandSchema);
angularjs express mongoose mean-stack
1个回答
1
投票

您必须在js文件中引用要在其中执行Schema查询的类别模式

以下是模型对象的方法,您可以执行以下任一操作来删除文档:

yourModelObject.findOneAndRemove(conditions, options, callback)

yourModelObject.findByIdAndRemove(id, options, callback)

还检查这个this它可能会帮助你

© www.soinside.com 2019 - 2024. All rights reserved.