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

Я использую 3.6.5 (mongodb) и пытаюсь получить документы рядом с указанным местоположением, например [-10, 20] ...

Когда я пытался получить запрос "http://localhost:3030/ninjas?lng=-80&lat=20", он возвращает" не удалось найти индекс для запроса $ geoNear "

Я попытался добавить index (), изменить запрос и найти официальный документ, но не смог.

пожалуйста, помогите!

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

app.use(bodyParser.json());

mongoose.connect("mongodb://localhost/gpsTest")
    .then(() => console.log('Connected to MongoDB...'))
    .catch(err => console.error(('Could not connect to MongoDB...\n'), err))


const NinjaSchema = new Schema({
    name: {
        type: String,
    },
    rank: {
        type: String,
    },
    available: {
        type: Boolean,
        default: false
    },
    geometry: {
        type: {
            type: String,
            default: "Point",
            index: '2dsphere'
        },
        coordinates: {
            type: [Number]
        }
    }
})
NinjaSchema.index({geometry: '2dsphere'});

const Ninja = mongoose.model('ninja', NinjaSchema);

app.post('/ninjas', (req, res) => {
    Ninja.create(req.body).then(ninja => {
        res.send(ninja);
    })
})

app.get('/ninjas', (req, res) => {
    Ninja.find({}).where('location').nearSphere({center: {
        type: 'Point',
        coordinates : [parseFloat(req.query.lng), parseFloat(req.query.lat)],
        spherical: true
    }}
    ).then(ninjas => {
        res.send(ninjas);
    });
})
app.listen(3030, () => {
    console.log(`listening port: 3030`);
})

Это для почтового запроса.

{"name": "test", "rank": "красный пояс", "available": true, "geometry": {"type": "Point", "координаты": [-80, 27]}}

1 Ответ

0 голосов
/ 03 сентября 2018

Это из-за опечатки ... Должно измениться

Ninja.find ({}). Where ('location'). NearSphere ({center: {...

)

до

Ninja.find ({}). Where ('геометрия'). NearSphere ({center: {...

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