Suppose you have a mongoose schema with a sub schema
const userSchema = new Schema({
name: {
type: Schema.Types.String
}
})
const eventSchema = new Schema({
name: {
type: Shema.Types.String,
},
user: {
type userSchema
}
})
const EventModel = new model(eventSchema);
This does not work
const event = new EventModel({ name: 'hi', user: { name: 'user' } });
event.user.name = 'new name';
await event.save();
// user.name is still 'user'
and this does not work
const event = await EventModel.find({_id: eventid});
event.user.name = 'new name'
await event.save()
const updated = await EventModel.find({_id: eventid});
console.log(updated.user.name); // not updated
and neither does this
const event = await EventModel.find({_id: eventid});
const keys = Object.keys(event.user);
keys.forEach(key => {
if(key === 'name'){
console.log('nope, does not exist.. just $__parent, and other ones');
}
})
so how are you supposed to use sub-schemas?
I tried to use subschemas and it did not work