I'm trying to setup a default array size and value in my mongoose schema, but the return is always appearing as [] unless the document actually has data.
"transform": { type: [ Number ], default: [0, 0, 0] }
Return value is:
"transform":[]
How do I configure my schema so it returns:
"transform":[0,0,0]
Answer:
Setting required: true will add the data to any new documents created. Old documents will not be updated, however.
"transform": { type: [ Number ], default: [0, 0, 0], required: true }
Update To quickly resolve my data issue I ran an update on Mongo to upgrade all existing documents using the following line.
db.getCollection('objects').updateMany(
{ "properties.transform": []},
{ $set: { "properties.transform" : [0,0,0] }}
);
NOTE: My transform field is a child of properties hence the "properties.transform"