I coming from a sql background so writing queries in sql where I join tables is quite simple but I guess I am missing that in mongoose/mongodb
Basically I know the S
You are just one step away!
Project Group Schema:
var ProjectGroupSchema = new Schema({
title : String
});
Project Schema:
var ProjectSchema = new Schema({
title : {type : String, default : '', required : true},
group : {type: Schema.Types.ObjectId, ref: 'ProjectGroup' },
_users : [{type: Schema.Types.ObjectId, ref: 'User' }]
});
User Schema:
var UserSchema = new Schema({
first_name : {type: String, required: true},
last_name : {type: String, required: true},
subscribing : [{type: Schema.Types.ObjectId, ref: 'Project' }]
});
Then you can do the following:
user.findById(req.userId)
.populate('subscribing')
.exec(function(err, user){
console.log(user.subscribing);
})
Or:
project.find({
subscriber : req.userId
})
.populate('subscriber')
.populate('group')
.exec(function(err, projects){
console.log(projects);
})
There are no joins in Mongodb. This question I think is a good reference:
MongoDB and "joins"
To summarize, different strategies have to be adopted with mongodb for problems that would be addressed via joins in relational DBs. I would say you mainly end-up doing one of these two things: