即使创建了索引,也无法找到$ geoNear查询的索引

use*_*558 2 mongoose mongodb node.js

我有以下架构,我在地理坐标上有一个二维索引.

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var LocationSchema = new Schema ({
    geo: {
        type: [Number],
        required: true,
        index: "2dsphere"
    }
});

module.exports = mongoose.model("Location", LocationSchema);
Run Code Online (Sandbox Code Playgroud)

我想找到新的位置:

Location.find({
    geo: {
        $nearSphere: [req.query.lat, req.query.lng],
        $maxDistance: maxDistance
    }
}, function(err, events) {
    if (err) {
        console.log(err);
        res.status(500).json(err);
    }
    res.status(200).json(events);
});
Run Code Online (Sandbox Code Playgroud)

该索引似乎已创建:

> db.system.indexes.find();
{ "v" : 1, "key" : { "geo" : "2dsphere" }, "name" : "geo_2dsphere", "ns" : "dev.events", "background" : true, "safe" : null, "2dsphereIndexVersion" : 2 }
Run Code Online (Sandbox Code Playgroud)

虽然我在这里创建索引,但是当我想要找到附近的位置时,我会收到以下错误:

{ [MongoError: Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0
Tree: GEONEAR  field=loc maxdist=0.156961 isNearSphere=0
Sort: {}
Proj: {}
planner returned error: unable to find index for $geoNear query]
name: 'MongoError',
message: 'Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0\nTree: GEONEAR  field=loc maxdist=0.156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
'$err': 'Unable to execute query: error processing query: ns=dev.locations limit=1000 skip=0\nTree: GEONEAR  field=loc maxdist=0.156961 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query',
code: 17007 }
Run Code Online (Sandbox Code Playgroud)

All*_*ktv 6

Mongo Docs Mongo使用[经度,纬度],在这里你使用反向.