How to get all the values by referenced Object?

I have the following Schemas.

var orgSchema = new Schema({
     name    : String,
     email   : String,
     webiste : String
});

var empSchema = new Schema({
     name    : String,
     email   : String,
     role    : String,
     org     : { type: Schema.Types.ObjectId, ref: 'org'}
});

var leaveSchema = new Schema({
     emp     : { type: Schema.Types.ObjectId, ref: 'emp'},
     from    : Date,
     to      : Date,
     reason  : String
});

empSchema having orgSchema Object and leaveSchema has references empSchema Object.

I tried myself

if get all the employees in a particular organization, i'm passing org object id in query param.

EmployeeModel.find(org: req.query.org, 
                   null , 
                   {limit: 10 , skip: 0 }, 
                   function(err, employees)(
   .....
   .....
));

This is working fine.

And now i'm trying to get all the leaves of employees in a particular organization. In the same way i'm passing org ObjectId in query param.

LeaveModel.find(org: req.query.org, 
                null , 
                {limit: 10 , skip: 0 }, 
                function(err, employees)(
   .....
   .....
));

But I stucked how to get that? Cause Leave referred by Employee Object only.

Could anyone help?

In your current schema, you can't do it in a single call. You can do this:

var ob={};
if(req.query.to)
       ob.to=req.query.to;
if(req.query.from)
       ob.from=req.query.from;
if(!req.query.emp){
   EmployeeModel.find(org: req.query.org, 
               null , 
               function(err, employees)({

    var empid_arr=employees.map(function(x){return x._id});
    //empid_arr has all _ids of emplyees in the org.
    //now getting all the leaves

    //building a dynamic query

    ob.emp={$in:empid_arr};


    LeaveModel.find(ob, 
            null , 
            {limit: 10 , skip: 0 }, 
            function(err, leaves){
    })
})
}else{
    ob.emp=req.query.emp;
    LeaveModel.find(ob, 
            null , 
            {limit: 10 , skip: 0 }, 
            function(err, leaves){
    })
}

You can't achieve this in a single call as leave and org has no relation in your schema. You have to go through employees.