DEV Community

Cover image for How to Create Relationships with Mongoose and Node.JS

How to Create Relationships with Mongoose and Node.JS

oluseyeo on September 15, 2020

FOCUS: One-to-many Relationships NoSQL databases, unlike SQL databases like PostgreSQL, MYSQL etc, which are traditionally built for ...
Collapse
 
idionomfon profile image
Idiono-mfon Anthony

Oh, this is a wonderful explanation. Thank you for spending time to make justice to this subject. I was researching ways of handling data modeling effectively in mongoDb and behold I landed In this, and it has cleared the air.

Collapse
 
adsadsasdasdasd profile image
Diego

Hi, How can I remove the id and _id repeated from the last image?

Collapse
 
giancode1 profile image
Giancarlo C

link

new Schema({ ... }, { id: false });

Collapse
 
sostenemunezero profile image
Sostene MUNEZERO BAGIRA

is it possible to use mongoose schema post hooks to add relation?

Collapse
 
oluseyeo profile image
oluseyeo

What kind of relationship are you looking to add?

Collapse
 
hemantparmar profile image
hemant-parmar

Great article. I am about to implement relations for my MEAN project.

A Couple of questions.
Q-1. Can I have multiple path in find().populate() method?
I have a Log collection, I was thinking to add 4 ObjectId type fields. i.e. client, service, executive, manager - of course apart from its own fields. Each of these are just one object. i.e. One Log entry will be associated with one Client, Service, Execute and Manager.

So I will need to add multiple path with their own select keys. Is that recommended?

And when on frontend, when the Log is displayed, I am planning to populate relevant fields from all these 4 (i.e. Client Name, ClientCategory, ClientSubCategory, ClientRating), (ServiceName, ServiceFreq), ExecName and ManagerName.

The Log display on frontend has Search and filter options on various fields. So when a user searches or applies filters the backend Mongoose query will run again, fetch the data and display.

Q-2: What would be the performance impact if the number of entries in Log collection is in the range to 5000 - 50,000, when I use Child Ref vs Parent Ref? Which one is recommended according to you in this case.

Thanks again
hemant

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

Great post ....thanks for this.
Question: Please how can i fetch data from db using timestamps. Thanks

Collapse
 
amincode24 profile image
Amin

This was a good tutorial.Goodjob!👍

Collapse
 
smahrt profile image
Kubiat Morgan

Nice write-up! You have done justice to the relationship options available to document-oriented DBs.
Easy to follow too.

Collapse
 
oluseyeo profile image
oluseyeo

Thank you, Kubiat.

Collapse
 
cca2013 profile image
Konstantinos Anastasiadis

Yes this is something clear , at last. It is the backend.
I would like to have the code and adjust it , to my front-end.
Thanks.

Collapse
 
faouzimohamed profile image
Faouzi Mohamed

Thank you for this wonderfull explanation. Now i can implement my own relations for my express Web App.

Collapse
 
hariszulfiqar054 profile image
hariszulfiqar054

Dude it's up to the mark. I just fall in love with your clear explanation. :)

Collapse
 
oluseyeo profile image
oluseyeo

Thank you.

Collapse
 
leramovsesyan profile image
SharkLera

This is very helpful, thank you!