Reputation: 390
I have the following typescript interface:
interface IDataItem {
id: string;
value: boolean | number | string;
temp: boolean;
}
For simplicity, let's consider a simple array of data like this:
const MyData: IDataItem[] = [
{
id: '1',
value: false,
temp: false,
},
{
id: '2',
value: 'Test',
temp: true,
},
{
id: '3',
value: 42,
temp: false,
},
];
How can I save this data to a MongoDB using mongoose? I have experience creating "Schemas" with typescript interfaces with "unique types" but not "alternative types" (such as boolean | number | string
).
To be more specific: What do I have to put where the question marks are?
const schema = new Schema<IData>({
id: { type: String, required: true },
value: ???,
temp: {type: Boolean, required: true },
});
Any suggestion is highly appreciated! Thank you so much!
Upvotes: 2
Views: 826
Reputation: 104
you can use mixed type for multiple types for a field it's like any(in Ts) probably not exactly what you want i guess but anyway:
value : {type : Schema.Types.Mixed}
also recommend to take a look at Mongoose Documentation for Mixed type
Upvotes: 1