有没有更快的方法来执行此 MongoDB 查询?

Is there a faster way to perform this MongoDB query?

我有两个集合,一个城市公交车跟踪应用程序的站点和服务。 以下是架构:

var ServiceSchema = new mongoose.Schema({
    name: {type: String, index: true},
    description: String,
    service_type: String,
    routes: {type: Array, "default": []}
});
上面的

"routes" 是一个对象数组,每个对象都有一个名为 "stops" 的字段,它是 stop_id 的数组,每个对象都索引到一个 Stop 文档。

var StopSchema = new mongoose.Schema({
    stop_id: {type: Number, index: true},
    name: String,
    identifier: String,
    locality: String,
    orientation: Number,
    direction: String,
    coordinates : {type: [Number], index: '2d'},
    destinations: { type: Array, "default": []},
    services: {type: Array, "default": []}
});

我想在给定服务中查找由 stop_id 索引的所有 Stop 文档。我使用了 for 循环。

var stops = db.services.findOne().routes[0].stops;
stops.forEach(function(j,k){
    printjson(db.stops.findOne({stop_id: j}));
});

这看起来是否正确,或者是否有更复杂(更快)的方法 我可以怎么做?

您可以使用 $in.

在一个 find 查询中获取路线中的所有站点

在shell中:

var route_stop_ids = db.services.findOne().routes[0].stops;
var stops = db.stops.find({stop_id: {$in: route_stop_ids}})