猫鼬模型中相同模式中的引用字段

时间:2020-11-04 11:56:58

标签: mongoose mongoose-schema mongoose-populate

我有以下架构:-

用户是主要模式,在“跟随”下是数组之一。在“跟随”模式中,“用户”是“引用”到“用户”模式。可以引用相同的模式吗?

请阅读我的查询评论...

const FollowersSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',   // Is it possible to use ref in the same Schema? Cause 'UserSchema as User' is the current parent Schema
    },
  },
  {
    timestamps: true,
  }
);

const FollowingSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User',
    },
  },
  {
    timestamps: true,
  }
);

const UserSchema = new mongoose.Schema(
  {
    full_name: {
      type: String,
      default: '',
    },
    dp: {
      type: String,
    },
    cover: {
      type: String,
    },
    view: {
      type: Number,
      default: 0,
    },
    email: {
      type: String,
    },
    bio: {
      type: String,
    },
    relationship: {
      type: String,
    },
    gender: {
      type: String,
    },
    social: [SocialSchema],
    education: [EduSchema],
    following: [FollowingSchema],
    followers: [FollowersSchema],
  },
  {
    timestamps: true,
  }
);

0 个答案:

没有答案