关系(1对1,1对多,多对多)在mongoose里如何实现

最好的办法还是写一个真实的项目,从博客项目开始。

了解关系(1对1,1对多)在mongoose里如何实现

  1. UserSchema = new Schema({
  2. ...
  3. contacts:[]
  4. });

了解关系(1对1,1对多,多对多)在mongoose里如何实现

  1. ContactSchema = new Schema({
  2. ...
  3. owner: {
  4. type: Schema.ObjectId,
  5. required: true,
  6. index: true
  7. }
  8. });

了解populate

  1. ContactSchema = new Schema({
  2. ...
  3. owner: {
  4. type: Schema.ObjectId,
  5. ref: user
  6. }
  7. });
  8. ContactSchema.find({}).populate(‘owner’).exec(callback);

关联真的好么?

从分库分表的角度讲