我有一个城市公交车跟踪应用程序的两个集合,停止和服务。 这是模式:
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_ids索引的所有Stop文档。我使用了for循环。
var stops = db.services.findOne().routes[0].stops;
stops.forEach(function(j,k){
printjson(db.stops.findOne({stop_id: j}));
});
这看起来是否正确,还是更复杂(更快) 我能做到的方式吗?
答案 0 :(得分:0)
您可以使用$in
在一个find
查询中获取路线中的所有站点。
在shell中:
var route_stop_ids = db.services.findOne().routes[0].stops;
var stops = db.stops.find({stop_id: {$in: route_stop_ids}})