如何在Mongoose中编写存储对象的模式

nimxete2  于 2023-04-12  发布在  Go
关注(0)|答案(1)|浏览(80)

我想存储一个对象,其中包含下面的例子对象的集合,

{
    basic1: { ischecked: false, ans: "" },
    basic2: { ischecked: false, ans: "" },
    basic3: { ischecked: false, ans: "" },
    basic4: { ischecked: false, ans: "" },
    basic5: { ischecked: false, ans: "" },
}

有人能告诉我,我如何写一个相同的模式存储在答案字段在下面的代码,

const BasicQ = mongoose.model("basicq", {
  userid: {
    type: mongoose.Types.ObjectId,
    required: true,
    ref: "Users",
  },
  answers: ???,
});

数据应该像这样存储在mongodb中

{
  _id: dad3434v,
  userid: dasdadad232,
  answers: {
    basic1: { },
    basic2: { },
    basic3: { },
    ....
  }
}
rxztt3cl

rxztt3cl1#

只是一个空对象,你可以存储这些所有对象

const BasicQ = mongoose.model("basicq", {
  userid: {
    type: mongoose.Types.ObjectId,
    required: true,
    ref: "Users",
  },
  answers: {},
});

相关问题