Such approach is not good for mongodb.You should create one collection that contains all data, and make only one query.
This is very important because:
- There is no transactions
- There is no joins
- There are only atomic operations on one document
- You will be not able to use aggregation framework
If it is inpossible to migrate to one collection schema then probably you should do as below:
var _ = require("underscore")._;
function get_all(cb) {
stops.find({}, function(err, stps, cb) {
stop_time.find({}, function(err, stptime) {
var all = _.map(stps, function(e) {
e.trip_id = stptime[e.stop_id].trip_id;
return e;
});
trips.find({}, function(err, trips) {
all = _.map(all, function(e) {
e.route_id = trips[e.trip_id].route_id;
return e;
});
route.find({}, function(err, routes) {
all = _.map(all, function(e) {
e.route_name = routes[e.route_id].route_name;
return e;
});
cb(all)
});
});
});
});
}
Be carefully, if there is a large amount of data in DB then you probably should use Cursors. http://mongoosejs.com/docs/api.html#querystream_QueryStream