【问题标题】:update associated collections in MEAN更新 MEAN 中的关联集合
【发布时间】:2019-08-03 00:57:55
【问题描述】:

我正在构建一个 MEAN 堆栈 Web 应用程序, 我有两个相关的集合 categoriesbrands

现在,当我添加一个新品牌并自动选择其类别时,类别 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】:

    您必须在要执行架构查询的 js 文件中引用 Categories 架构,然后

    以下是 model 对象上的方法,您可以执行以下任何操作来删除文档:

    yourModelObject.findOneAndRemove(conditions, options, callback)
    
    yourModelObject.findByIdAndRemove(id, options, callback)
    

    也检查一下这个this它可能对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2016-01-13
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多