Greetings :)
I'm really enjoying using Mongoose in the MEAN stack I'm developing with! Kudos to the teams working on all this great software! I'm having one issue that I've been wresting all day and can't seem to make work, no matter how much I've scoured the documentation I could find online.
Basically, I have an existing schema and existing data in the database. I want to add a new field to the schema, which is an array of strings. Then, when I write any existing records back as an update, I want to be able to add the new field information to those records. I know this can easily be done in MongoDB itself, but I very much like the schema system that Mongoose provides, so I don't want to eliminate it at all. It just seems that it must be possible to do this, since even relational DBs can add a new field to a table and allow you to start filling the data into existing rows.
I've tried simply adding the new field to the schema, but it gets ignored by Mongoose when I do save. Then, I've also specified the "add" method on the schema, but same result. Then, I specified "strict: false" on the schema, but still the same thing. I also tried doing a save specifying { $set: { workUnits: ['value']}} as the body, but that didn't work either. Here is the schema as it now stands.
var busUnitSchema = new Schema({
uniqueId: { type: String, unique: true },
name: { type: String, index: true },
tags: [String],
companyUnit: { type: Schema.Types.ObjectId, ref: 'BusUnit'},
parentUnit: { type: Schema.Types.ObjectId, ref: 'BusUnit'},
billing: [{
amount: Number,
payType: { type: String, enum: consts.PAY_TYPE },
payFreq: { type: String, enum: consts.PAY_FREQ },
chargeToUnit: { type: Schema.Types.ObjectId, ref: 'BusUnit'},
ccNumber: String,
expDate: Date,
nameOnCard: String
}],
contactUser: { type: Schema.Types.ObjectId, ref: 'User' },
contactEmail: String,
workUnits: [String],
lastUpdatedBy: { type: Schema.Types.ObjectId, ref: 'User' }
},{strict: false});
busUnitSchema.add({ workUnits: [String] },'');
I'm thinking that if this is not something supported, that is a huge hole for actually being pleasant to use in production. If that IS the case, I'm interested in people's thoughts on what I can do to make it work, perhaps developing a plugin or something if needed.
Many thanks for any help on this :)