Adding a model as a subdocument in another model which is in a separate file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Adding a model as a subdocument in another model which is in a separate file

I am using having serious issues with performing this task. Can someone put me through on how it is done. I use mongoose and node.js

2nd Dec 2020, 10:44 PM
Catalyst
Catalyst - avatar
2 Answers
0
In the model you want to add to, whatever field your adding to should be an array of objectsId's that reference the the other model you want added. For example, add a new friend: USER MODEL ...... friend: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User'}], ..... Then in your /add-friend route, you update the model by pushing the friends ID to the friend array. https://stackoverflow.com/questions/18001478/referencing-another-schema-in-mongoose#18002078
16th Dec 2020, 5:21 PM
Jonny Legend
0
Thanks you very much Jonny Legend
21st Jan 2021, 8:37 PM
Catalyst
Catalyst - avatar