I have two models in my mongoDB. One is user model and one is event model. What I want to achieve is, when I will add a new event then, that event array will save to my user model's events column array. Till now, I've successfully saved that id of that event to specific user but the problem is only the **objectID** is coming not the property is showing up likeevent name,descriptions`. This is my code:
user model:
const userSchema = mongoose.Schema({
name: String,
events: [
{
type: Schema.Types.ObjectId,
ref: 'Event'
}
]
});
Event model:
const eventSchema = new Schema({
creator: {
type: Schema.Types.ObjectId,
ref: 'User'
},
ename: {
type: String
},
description: {
type: String
},
});
routes.js
router.post('/', (req, res) => {
const event = new Event();
event.creator = '5d9e1ba694f44227e1f54cc0',
event.ename = req.body.ename;
event.save( (err, result) => {
if (!err)
res.redirect('events');
else {
console.log('error', err);
}
});
User.findOneAndUpdate('5d9e1ba694f44227e1f54cc0', {$push: { events: [event] }}, (err, result) => {
if(err) {
console.log(err);
} else {
console.log('event is saved');
}
});
});
for simplicity, I've kept my user id hardcoded. Now, how can I update the $push method of the mongodb so that the whole properties of the event is to save in the user model not only the objectID