I have a one to many relational documents and I would like them save each other ids but somehow my code is not working. Please let me know what mistake I am making here
this is how user model looks like in my codebase
const userSchema = new Schema({
first_name: String,
...
products: [{ type: Schema.Types.ObjectId, ref: 'Product' }]
});
const UserModel = model('User', userSchema);
product model
const productSchema = new Schema({
name: string,
...
user_id : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
const ProductModel = model('Product', productSchema);
saving data in controller
const savedUser = await new UserModel({ ...user }).save();
product.user_id = savedUser._id;
const savedProduct = await new ProductModel(product).save();
savedUser.update({ $push: { products: savedProduct._id } });
console.log(savedUser.populated('products')); // undefined