'update Mongoose Model with new schmea

  1. I have a mongoose schema named child.

     child = {
     "IsActive": {
         type: Boolean,
         default: true
     },
     "IsDeleted": {
         type: Boolean,
         default: false
     } }
    
  2. based on this schema a mongoose model is created

let customerObject = mongoose.model("Initial", child);

  1. Now, in some scenarios I need to update the child schema (e.g default : true for "isDeleted" only) after step 2.

     child = {
     "IsActive": {
         type: Boolean,
         default: true
     },
     "IsDeleted": {
         type: Boolean,
         default: true
     } }
    

Question: Is it possible to update the already created customerObject model with the latest schema? (i.e child from step3 )

It may be irrelevant but I tried to call the same model again after step 3, as

let customerObject = mongoose.model("Initial", child);

hoping that customerObject model can be created again with latest child schema, but I get the following error message in this case:

OverwriteModelError: Cannot overwrite `Initial` model once compiled.


Solution 1:[1]

I suspect $decodedData->descriptions is an array; try accessing the first element of that array with square brackets: $decodedData->descriptions[0]->name;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MaartenDev