英文:
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: {},
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论