DEV Community

Cover image for How Create Relationships with Mongoose and Node.js ( with real example )

How Create Relationships with Mongoose and Node.js ( with real example )

MKilmer on October 20, 2019

Before all, not exists JOIN here! Basically is saved the _id from one collection to another collection ( {type: mongoose.Schema.Types.ObjectId,ref:...
Collapse
 
jsardev profile image
Jakub Sarnowski

My answer would be: don't. If you need relationships, go for a relational database. IMHO NoSQL databases are for totally different purpose: they're for data, where you don't care about the structure, like logs, analytics data etc. In case you need a structure, NoSQL database is a bad idea because the database doesn't give you any certainty about how the data is structured.

Collapse
 
mkilmer profile image
MKilmer

Thanks for your reply.I know about this, but i just only wanted to show that possible create relationships with NoSQL,even though it's not the best solution for building relationships. 🀘 🀘 🀘

Collapse
 
jsardev profile image
Jakub Sarnowski

Sure thing! I don't say your article is bad, it helps a lot! Just wanted to point out my concerns πŸ˜„

Collapse
 
samantafluture profile image
Samanta Fluture

Awesome tutorial!

Just adding how I managed to work in a slightly different way.

My 'create' function from my post controller looks like this:

create : async (req, res) => {

        const user = req.params.id;
        const { title, subtitle} = req.body;
        const post = await Post.create({
            title,
            subtitle,
            user
        });
        await post.save();

        const userById = await User.findById(user);

        userById.posts.push(post);
        await userById.save();

        return res.send(userById);
    },
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lagsurfer profile image
Barney

Could you please add request examples?

Collapse
 
mkilmer profile image
MKilmer

I added a gif, showing how the routes work. Thanks for the feedback! πŸ˜„

Collapse
 
lagsurfer profile image
Barney

Thank you very much! :) cool post

Thread Thread
 
mkilmer profile image
MKilmer

🀘 🀘 🀘

Thread Thread
 
robotfights profile image
Emmanuel

Hi, am having an issue with my mongoose command installation on windows terminal: npm install --save mongoose

each time i try to install i get a node file opened from vnode.exe.

any help would be appreciated

Thread Thread
 
xvoodooxl profile image
Fermin Solis

Just do npm install mongoose, the save is not needed anymore since versiΓ³n 5.x.x I think. Some one correct me if im wrong.

Collapse
 
manojap profile image
MANOJ AP

router.post('/user/find',User.find); this line not working

Collapse
 
berkslv profile image
berkslv

Thank you sooo much! mongose docs isn't enough for this :(

Collapse
 
oluseyeo profile image
oluseyeo

Thank you, this was excellent.

Collapse
 
djdole profile image
DJDole • Edited

While an interesting exercise, anyone looking production solutions, reconsider.
Never reinvent the wheel.
If you need RDBS, choose RDBS.
Don't shoehorn RDBS into a document-oriented DBS.

Collapse
 
mkilmer profile image
MKilmer

Thanks for your reply! Yes, it is for teaching purposes only, when you want to create relationships, I would not recommend using NoSQL. 🀘 🀘 🀘

Collapse
 
vmuthabuku profile image
vincent muthabuku

also why did you use the router.post request on user/find and user/find/post/:id? Just curious, otherwise great article.
O

Collapse
 
avoidinglime profile image
AvoidingLime

Hello, I know this is a post from 2019 but I am following in 2021 - having an issue with the create Post POST - can you show us a POSTMAN example of what ID you used to POST the post?