Set mongoose field as type of either String or Number

Viewed 3440

I have a field example in my database, which I want it to be either Number or String:

export const mySch = new mongoose.Schema({
    example: {
        type: String,
    },
});

How can I do it?

2 Answers

There is a type in mongoose Schema.Types.Mixed which can be used to save values of any type in the given field. However, I haven't yet found a way to restrict it to just a particular subset of types e.g String and Number.

Here is the link if you want to study in detail yourself: https://mongoosejs.com/docs/schematypes.html#mixed.

Related