首页 > 解决方案 > 您可以创建一个虚拟而不是在模型(猫鼬)中添加直接引用吗?

问题描述

如果我有 Board 模型和 List 模型,并且想在获取列表时访问 boardId,我可以这样做:

const BoardSchema = new Schema(
  {
    title: {
      type: String,
      trim: true,
      required: [true, "The Board title is required"],
    },
  },
  { timestamps: true }
);

const ListSchema = new Schema(
  {
    title: {
      type: String,
      trim: true,
      required: [true, "The List title is required"],
    },
    boardId: {
      type: ObjectId,
      ref: "Board",
    },
  },
  { timestamps: true }
);

我可以通过使用虚拟属性而不boardId直接添加到架构来做同样的事情吗?

标签: mongodbmongoose

解决方案


推荐阅读