Define variant type in Mongoose Define variant type in Mongoose mongoose mongoose

Define variant type in Mongoose


I think mongoose wants you to use a nested type to declare a property as an object. Using your foo type above:

var PostSchema = new mongoose.Schema({   num: [Number],   name: [String],   variant: {      fooInt: [Number]      fooPair: {         fooInt: [Number],         fooString: [String]      }    } });

Or - you could punt and use mixed - but that seems really wishy-washy to me.

Or - in the true spirit of oCaml - you can have that var defined using a pattern (type+object):

 ... variant: {    varType: [Number],    varInfo: [Mixed] }

Where varInfo's structure depends on varType. THAT would make your mongo queries eaiser to manage.

Hope that's the direction you were looking for! FYI - I find this simple post really helpful.


You can use this code see the mongo documentation herehttps://docs.mongodb.com/manual/reference/operator/query/type/

var PostSchema = new mongoose.Schema({    num: { type: Number },    name: { type: String },     variant: { $type: [ Int: “int” , Pair:[“int”, “string” ] }