1

该集合有两个地理字段:fromLocation 和 toLocation。但只允许使用一个 Geonear。该集合看起来像:

...............
 fromLocation: {
    type: { type: String, default: "Point" },
    coordinates: [Number],
  },
  toLocation: {
    type: { type: String, default: "Point" },
    coordinates: [Number],
  },
.........................................

请给出示例代码,如何使用 Geonear 通过两个字段进行搜索。我的单场搜索代码:

[
  {
    '$geoNear': {
      near: [Object],
      key: 'fromLocation',
      distanceField: 'fromDistance',
      spherical: true
    }
  },
  {
    '$match': {
      status: 2,
      'from.data.city_fias_id': '27c5bc66-61bf-4a17-b0cd-ca0eb64192d6',
      'to.data.city_fias_id': '27c5bc66-61bf-4a17-b0cd-ca0eb64192d6',
      'car.paymentInfo.id': [Object],
      budget: [Object]
    }
  },
  {
    '$lookup': {
      from: 'users',
      localField: 'autor',
      foreignField: '_id',
      as: 'autor'
    }
  },
  { '$unwind': '$autor' },
  { '$addFields': { sortBudget: [Object] } },
  { '$sort': { sortBudget: 1 } },
  { '$group': { _id: null, total: [Object], results: [Object] } },
  { '$project': { total: 1, results: [Object] } }
]
4

1 回答 1

0

距离计算相当简单。将 $geoNear 用于更具选择性的条件以利用地理索引,将 $match 和 $expr 用于第二个条件。

于 2020-08-14T17:15:07.353 回答