DEV Community

Discussion on: Designing a better architecture for a Node.js API

Collapse
 
pacheco profile image
Thiago Pacheco

Hi Rodrigo, I just downloaded the source code again, tested it and everything looks fine.
Can you tell me which problems you ran through, maybe I can help you.

Collapse
 
lalwanikamaldev profile image
lalwanikamaldev

HI Thiago ,

I am new to node js and UI development and i am using this architecture for development .

I am stuck to joining 2 table and get the result set . let me know what is the right way to join the table and get the result set from it .
i have 2 table post and comment where comment is having relationship with post .
Looking forward for your reply ...

Thread Thread
 
lalwanikamaldev profile image
lalwanikamaldev

Any Idea ?

Thread Thread
 
pacheco profile image
Thiago Pacheco

Hello there,
Sorry for the delay, I thought I had answer you already.

You could basically define a comments attribute into your post schema with a reference for this comment schema, like so:

{
 ...your attributes
  comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }]
});
Enter fullscreen mode Exit fullscreen mode

Then when you query this post, you can use the populate method from mongoose to retrieve the comments.

Post.
  findOne({ title: 'title' }).
  populate('comments')
Enter fullscreen mode Exit fullscreen mode

Here is the link with the docs about how you can implement a relationship one-to-many.
mongoosejs.com/docs/populate.html

Thread Thread
 
pacheco profile image
Thiago Pacheco

But remember that this is not the same concept of joining as in SQL, in MongoDB you don't have this concept of relationship.
The relationship is defined by code, with your schemas and the extra validations you are supposed to create based on your necessities.

Here is some more information about the key differences between NoSQL and SQL:
mongodb.com/nosql-explained/nosql-...