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
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:
If it is inpossible to migrate to one collection schema then probably you should do as below:
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