Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify schema in mongoose

I was wondering what the best way to define this schema is:

enter image description here

Newsitem can only have 1 userID and 1 task ID. A Task can belong to many newsitems. A user can belong to many newsitems

I've searched for examples, but I'd say they are not a lot of examples. How can I do this with mongoose & mongodb ? I'd say that both User to newsitem and Task to Newsitem are one to many relations.

like image 439
Olivier_s_j Avatar asked Jan 31 '26 04:01

Olivier_s_j


1 Answers

In Mongodb you can reference a collection within a schema so when you find() a document in newsitem mongoose does a find() in user and task, so instead of returning the newsitem document it returns the newsitem with the user and the task.

Mongoose population

like image 89
Gabriel Llamas Avatar answered Feb 02 '26 19:02

Gabriel Llamas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!