Sunday 11 December 2016

Some clarification on relationships in databases.

Hello currently I am using mongodb which I am getting ready to switch to postgresql, just because of the fact that I am using mongodb more and more like a relational database than what its intended for. So I might as well use a database meant for relational queries.So a user can have a profile, which other users can come to and comment to that profile. Other users can reply to comments in others profile.This is where the confusion sits in with my relation ships. Here are my tables I am working with. user: _id: username: password: profile_img: profile: _id: user_id: comments_id: comment: _id: user_id: profile_id: replies_id: body: created_at: reply: _id: user_id: comment_id: body: created_at: so right now I can use mongoose's populate command to pull out the comments and replies of a profile, I run into the issue when I want to show the reply.user.profile_img. The only information available from the original find query on comments is reply.user_idHow do people normally join multiple queries together to get all the information they need?Are my sql tables too bloated? Does it need to be condensed more?here is my current mongoose query to find the comments and display it with their replies. I just cant get any user information from replies. I dont now how to go deeper and still keep the replies linked to the proper commentsapp.get('/p/:user/comments', function(req, res){ User.findOne({ username: req.params.user }, function(err, profileUser){ Profile.findOne({ _user: profileUser._id }, function(err, profile){ ProfileComment.find({ _profile: profileUser._id }).sort('-created_at').populate('_replies').populate('_user').exec(function(err, comments){ res.render('comments.html', { title: req.params.user + ' Comments', messages: req.flash('alert'), user: req.session.user, profileImage: profileUser.profileImage, profileComments: comments, isLogged: req.session.isLogged, userProfile: req.params.user }); }); }); }); }); This is the rendering html file Thanks for any guidance and help!

Submitted December 11, 2016 at 06:20PM by clandest

No comments:

Post a Comment