Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I am defining a mongoose schema and definition is as follows:

   inventoryDetails: {

        type: Object,

        required: true

    },

    isActive:{

        type:Boolean,

        default:false

    }

I tried the "Object" type and I am seeing my data is getting saved successfully. When I changed the type to the array, the save is failing.

Sample Data:

{

    "inventoryDetails" : { 

        "config" : { 

            "count" : { 

                "static" : { "value" : "123" }, 

                "dataSource" : "STATIC" 

            }, 

            "title" : { 

                "static" : { "value" : "tik" }, 

                "dataSource" : "STATIC" 

            } 

        }, 

        "type" : "s-card-with-title-count" 

    } 

}

"Object" type is not one of the types that mongoose allows. But, how it is being supported?

1 Answer

0 votes
by (107k points)

Use the default Schema.Types.Mixed type

The following example is taken from the following link:

https://mongoosejs.com/docs/schematypes.html

let YourSchema = new Schema({

  inventoryDetails: Schema.Types.Mixed

})

let yourSchema = new YourSchema;

yourSchema.inventoryDetails = { any: { thing: 'ABC' } }

yourSchema.save()

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...