如何使用mongoose,Node.js删除层次结构?

时间:2018-01-16 07:41:59

标签: node.js express mongoose

我的架构层次是...... 的 List -> Items -> (attachements,comments,labels)

Que:所以如果我想删除一个列表,所有引用的(Hierarchical)元素应该删除 .Here(附件,注释,标签)被引用到< strong>项目和项目引用列表。所以请给我建议。 架构:

List:{
 list_title: {type: String, required: true},
    created_at: Date,
    updated_at: Date
}
Item:{
 item_title: {type: String, required: true},
  discription:{type: String},
  label : {type:Array ,label_id : String },
  created_at: Date,
  updated_at: Date,
  _list: {type: Schema.Types.ObjectId, ref:'List'}
}
Comment:{
  comment:{type:String},
  created_at: Date,
  updated_at: Date,
  entity_id : {type: Schema.Types.ObjectId, ref:''},
  model : {type : String ,enum : ['Item','File'] }
}
Attachment:{
   title : [{type : String}],
     // files: [String],
       path : [String],
      make_cover : {type : Boolean },
    _item: {type: Schema.Types.ObjectId, ref:'Item'}
    }

提前致谢

1 个答案:

答案 0 :(得分:0)

使用Mongoose进行搜索时,会收到Mongoose对象。每个Mongoose对象都包含它所代表的文档中存储的所有信息(list - &gt; Items - &gt;(附件,注释,标签) 如果你有想要在Node.js中使用的对象,你可以在对象中删除你不再需要的所有元素,最后,你可以调用.save()方法和你所做的所有更改在对象中创建的将保存在MongoDB中

示例:

myCollection.findOne({name: "myName"},  function(err, obj) {
    // obj is the Mongoose object with all the information in the document in DB
    // Here you can remove elements from obj, imagine you want to remove first item in the first element of List
    obj.List[0].splice(0, 1);
    // Finally, we save changes in DB
    obj.save();
})