MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

La query Mongoose/mongoDB si unisce... ma vengo da uno sfondo sql

Sei solo ad un passo!

Schema del gruppo di progetti:

var ProjectGroupSchema = new Schema({
    title             : String
});

Schema del progetto:

var ProjectSchema = new Schema({
    title         : {type : String, default : '', required : true},
    group         : {type: Schema.Types.ObjectId, ref: 'ProjectGroup' },
    _users    : [{type: Schema.Types.ObjectId, ref: 'User' }]
});

Schema utente:

var UserSchema = new Schema({
    first_name    : {type: String, required: true},
    last_name     : {type: String, required: true},
    subscribing   : [{type: Schema.Types.ObjectId, ref: 'Project' }]
});

Quindi puoi fare quanto segue:

user.findById(req.userId)
     .populate('subscribing')
     .exec(function(err, user){
          console.log(user.subscribing);
     })

Oppure:

project.find({
        subscriber : req.userId
      })
     .populate('subscriber')
     .populate('group')
     .exec(function(err, projects){
          console.log(projects);
     })