小编use*_*558的帖子

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

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

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)

mongoose mongodb node.js

2
推荐指数
1
解决办法
4827
查看次数

标签 统计

mongodb ×1

mongoose ×1

node.js ×1