4

I am working one project there is some relational concept so i want to retrieve data from two different tables in same query , can i do this ? i am following below link but i am not getting exact that i wants. Mongoose/mongoDB query joins.. but I come from a sql background

I have also review this links

https://start.jcolemorrison.com/mongodb-joins-with-mongoosejs/

I have review this mongoose links, here i am getting something positive.

http://mongoosejs.com/docs/populate.html

What i have done so far is like below: Create Models for project and project_category and try to get all project with category details. I have update my code at below link to review :

https://www.protectedtext.com/rushabhcode

Rushabh Madhu
  • 645
  • 2
  • 7
  • 19

1 Answers1

3

mongooses have populate in built function to join two table

in schema just add ref for linking table

suppose you want to join two table table 1 :User table 2 :posts

in posts schema add

posted_by: {type: Schema.Types.ObjectId, ref: 'User', required: true},

and the time of query just add populate

suppose

postModel.find({}).populate({path:'posted_by'}).exec(function(err,response){


})

this will help you get all post value with user details who has posted