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

huangapple go评论63阅读模式
英文:

How to write schema in mongoose for storing object of objects

问题

{
我想要存储一个包含对象集合的对象,就像下面的示例一样,

{
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: { },
....
}
}
}

英文:

I want to store an Object which contains collection of object like below example,

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

Can someone tell me how can i write a schema for the same to store in answers field in the below code,

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

The the data should store in mongodb something like this,

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

答案1

得分: 1

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

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

Just an empty object, u can store these all object

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

huangapple
  • 本文由 发表于 2023年4月11日 16:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定