【发布时间】:2021-12-31 07:16:13
【问题描述】:
我有以下打字稿界面:
interface IDataItem {
id: string;
value: boolean | number | string;
temp: boolean;
}
为简单起见,让我们考虑一个简单的数据数组,如下所示:
const MyData: IDataItem[] = [
{
id: '1',
value: false,
temp: false,
},
{
id: '2',
value: 'Test',
temp: true,
},
{
id: '3',
value: 42,
temp: false,
},
];
如何使用 mongoose 将这些数据保存到 MongoDB?我有使用带有“唯一类型”但不是“替代类型”(例如boolean | number | string)的打字稿接口创建“架构”的经验。
更具体地说:我必须在问号所在的位置放什么?
const schema = new Schema<IData>({
id: { type: String, required: true },
value: ???,
temp: {type: Boolean, required: true },
});
非常感谢任何建议!非常感谢!
【问题讨论】:
标签: typescript mongodb mongoose