我正在使用 node.js 和 Mongoose 开发我的应用程序。我有一个名为 categories
的模式,它包含一个名为 content
的键,我需要为其分配不同类型的对象,如下所示。
const categorySchema = new Schema({
label: { type: String, required: true },
content: [
{ type: Schema.Types.ObjectId, ref: "Videos" },
{ type: Schema.Types.ObjectId, ref: "Games" }
]
});
所以 typescript 中的对象类型将是
content: Array<VideosType | GamesType>
知道我该怎么做吗?
最佳答案
您可以使用Schema.Types.Mixed
https://mongoosejs.com/docs/schematypes.html#mixed :
const categorySchema = new Schema({
label: { type: String, required: true },
content: [{ type: Schema.Types.Mixed }]
});
关于javascript - mongodb 模式中具有不同对象类型的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59137833/