Javascript nodejsmongoose复杂查询

Javascript nodejsmongoose复杂查询,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我就是想不起来。我正在尝试在mongoose中获取查询结果。我有一个行程数据库,其中包括一条停车路线。我想获得所有访问目标站点的旅程(额外好处:使用特定平台) 以下是模式的外观: var StopSchema = new Schema({ arrival: String, departure: String, platform: String, station: String, }); var JourneySchema = new Schema({ trainNumber

我就是想不起来。我正在尝试在mongoose中获取查询结果。我有一个行程数据库,其中包括一条停车路线。我想获得所有访问目标站点的旅程(额外好处:使用特定平台)

以下是模式的外观:

var StopSchema = new Schema({
  arrival: String,
  departure: String,
  platform: String,
  station: String,
});

var JourneySchema = new Schema({
  trainNumber: String,
  destination: String,
  route: [StopSchema]
});
示例数据:

{
  trainNumber: '1',
  destination: 'Z',
  route: [
  { arrival: "11:23", departure: "11:25", platform: "3", station: "A"},
  { arrival: "11:33", departure: "11:35", platform: "3", station: "B"},
  { arrival: "11:43", departure: "11:45", platform: "3", station: "Z"}
  ]
},
{
  trainNumber: '2',
  destination: 'Z',
  route: [
  { arrival: "12:23", departure: "12:25", platform: "3", station: "A"},
  { arrival: "12:33", departure: "12:35", platform: "3", station: "B"},
  { arrival: "12:43", departure: "12:45", platform: "3", station: "Z"}
  ]
},
{
  trainNumber: '3',
  destination: 'F',
  route: [
  { arrival: "12:23", departure: "12:25", platform: "3", station: "D"},
  { arrival: "12:33", departure: "12:35", platform: "3", station: "E"},
  { arrival: "12:43", departure: "12:45", platform: "3", station: "Z"}
  ] 
}
请求:获取访问“B”(3号站台)的所有行程,列出路线并推广目标站点数据

期望结果:

[
{
  trainNumber: '1',
  destination: 'Z',
  route:  [
  { arrival: "11:23", departure: "11:25", platform: "3", station: "A"},
  { arrival: "11:33", departure: "11:35", platform: "3", station: "B"},
  { arrival: "11:43", departure: "11:45", platform: "3", station: "Z"}
  ],
  targetStation: {  arrival: "11:33", departure: "11:35", platform: "3", station: "B"}
},
{
  trainNumber: '2',
  destination: 'Z',
  route: [
  { arrival: "12:23", departure: "12:25", platform: "3", station: "A"},
  { arrival: "12:33", departure: "12:35", platform: "3", station: "B"},
  { arrival: "12:43", departure: "12:45", platform: "3", station: "Z"}
  ],
  targetStation: {  arrival: "12:33", departure: "12:35", platform: "3", station: "B"}
}
]

我只是不知道我可以使用什么样的elemmatch/aggregate/virtual/query邪恶组合。

因为MongoDB不支持连接,所以您不能用所需的模式在单个查询中完成这项工作。您至少需要两个查询:一个用于获取目标站点的ID,然后第二个用于获取具有该站点的旅程。类似于(假设模型
停止
旅程
):


好的,所以我需要分多个步骤来做。但是从一站开始是很奇怪的,因为我需要在那个站完成所有的行程。我将围绕您的答案中的片段进行讨论,谢谢。您的示例显示了一个目标停止,因此提供了示例代码。第二个查询获取包括该站点的所有行程。也就是说,如果您想要查询多个站点,那么让它查询多个站点是很简单的。
Stop.findOne({station: 'B', platform: '3'}).exec().then(function(stop) {
  if (stop === null) { throw new Error('No stop matches request');
  return Journey.find({route: {_id: stop.id}}).populate('route').exec();
}).then(function(journeys) {
  if (journeys === null || journeys.length === 0) { throw new Error('No journeys include requested stop'); }

  // `journeys` should be an array matching your desired output
  //  you can add the extra property here or in the journeys query if you wish
}).then(null, function (err) {
  // Handle errors
});