使用mongodb和mongoose

时间:2016-01-29 00:38:39

标签: mongodb mongoose nosql

我仍然无法理解像mongodb这样的NoSQL数据库。 我正在使用这组不同的Mongoose模型创建一个项目:

部门模型:

var DepartmentSchema = new mongoose.Schema({
  name: String,
  info: String,
  active: Boolean,
  categories : [{ type : mongoose.Schema.Types.ObjectId, ref : 'Category', required : true}]
});

类别模型:

var CategorySchema = new mongoose.Schema({
  name: String,
  info: String,
  active: Boolean,
  icon : String,
  products : [{ type : Schema.Types.ObjectId, ref : 'Category'}]
});

产品型号:

var ProductSchema = new mongoose.Schema({
  name: String,
  info: String,
  active: Boolean,
  imgUrl : String,
  category : {
    type : Schema.Types.ObjectId, ref:'Category', required : true
  }
});

用户模型:

var UserSchema = new Schema({
  name: { 
    type: String
  },
  requests : [{ type : Schema.Types.ObjectId, ref : 'Request'}],

});

申请型号:

var RequestSchema = new mongoose.Schema({
  name: String,
  info: String,
  active: Boolean,
  user : {type:mongoose.Schema.Types.ObjectId, ref : 'User', required : true},
  product : {type:mongoose.Schema.Types.ObjectId, ref : 'Product', required : true},
  fulfilled : { type : Boolean, default : false }
});

基本上,部门可以有多个类别,一个类别可以有多个产品,用户可以创建请求,而请求几乎是产品和用户之间的链接。

我知道这是一种更关系的方法所以我并不真正关心更新引用的模式,当我需要删除类别时会出现问题,例如,产品仍然存在,因此我必须手动删除每个产品(并因此请求)该特定类别。

现在,如果我尝试使用嵌入式方法将数据作为整个对象(包含部门,类别和产品),这不是问题,因为我可以删除数组的一部分,问题出现在我创建的时候请求,我必须将产品从Department对象复制到请求中,然后,如果我更新该对象内的产品,我将如何跟踪更新以及我已经保存在其他对象中的请求文件?

如果有帮助,我正在使用yeoman生成器(angular-fullstack)来生成模型,控制器,端点或实体的路由。

谢谢。

0 个答案:

没有答案