Mongoose: невозможно найти индекс для запроса $ geoNear, даже если индекс доступен и запрос работает в mongodb - PullRequest
0 голосов
/ 21 сентября 2018

Проблема: mongoose не может обнаружить индекс '2dsphere'

Вот так выглядит моя схема:

export const ProviderSchema = new Schema({
  address: {
    location: {
      coordinates: {
        required: true,
        type: [Number],
      },
      type: {
        enum: ['Point'],
        required: true,
        type: String,
      },
    },
  },
}, {
  timestamps: true,
});

ProviderSchema.index({ 'address.location': '2dsphere' });
export const Provider = mongoose.model('Provider', ProviderSchema);

Так выглядит мой агрегат:

return Provider
  .aggregate([
    {
      $geoNear: {
        distanceField: 'address.distance',
        near: [parseFloat(lng), parseFloat(lat)],
        query,
      },
    },
  ])

Я знаю, что моя коллекция проиндексирована, потому что Provider.listIndexes() возвращает этот результат:

[
  { v: 2, key: { _id: 1 }, name: '_id_', ns: 'patients.providers' },
  { v: 2,
    key: { 'address.location': '2dsphere' },
    name: 'address.location_2dsphere',
    ns: 'patients.providers',
    background: true,
    '2dsphereIndexVersion': 3
  }
]

И когда я запускаю этот запрос непосредственно на mongodb, он работает как ожидалось:

db.providers.aggregate([
    {
        $geoNear: {
            near: { type: 'Point', coordinates: [-104,39]},
            distanceField: 'address.distance',
            spherical: true
        }
    }
])

Может кто-нибудь помочь мне указать, что я делаю неправильно, или это определенно ошибка?

версия: "mongodb": "^ 3.1.6", "mongoose": "^5.2.16" ,

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...