Guillaume le Floch Guillaume le Floch - 2 months ago 13
Javascript Question

Multiple queries in mongoDB

I would like to query multiple collections in mongodb, I have some data like this

Collection stops {
{ stop_id : 1, stop_name: 'a'},
{ stop_id : 2, stop_name: 'b'}, ...

Collection stop_time {
{ stop_id : 1, trip_id: 40},
{ stop_id : 2, trip_id: 41}, ...

Collection trips {
{ trip_id : 40, route_id: 400},
{ trip_id : 41, route_id: 401}, ...

Collection route {
{ route_id : 400, route_name: 'foo'},
{ route_id : 401, route_name: 'bar'}, ...


And for each stop_name, I would like to know the route_name that match with it... (I didn't chose he data-structure by the way, that's GTFS format...)

Is there a way to easily do a queries that respond to my problem?

Thanks

Answer

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:

  1. There is no transactions
  2. There is no joins
  3. There are only atomic operations on one document
  4. 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

Comments