在 MongoDB 上创建地理空间索引

Creating a Geospatial Index on MongoDB

我正在使用 MongoDB 的地理空间特征,我的典型文档如下所示:

{
    "_id" : ObjectId("574ebe5f9985eb31a9bdbe39"),
    "geoareaName" : "GEOAREA2",
    "loc" : {
        "type" : "Polygon",
        "coordinates" : [
            [
                [
                    13.870663396091308,
                    3.481292724609375
                ],
                [
                    13.875996314658366,
                    3.482236862182617
                ],
                [
                    13.872746581997381,
                    3.484210968017578
                ],
                [
                    13.871829982503106,
                    3.484961986541748
                ],
                [
                    13.870663396091308,
                    3.481292724609375
                ]
            ]
        ]
    },
    "color" : "#ff0000",
    "_version" : 4,
    "active" : false
}

然后我使用 $geoIntersects 关键字查询了一些点位置,它非常有用。但是,为了最佳地使用 mongo 的地理空间特征,我想在该位置周围放置索引。

有人可以建议正确的方法吗?

loc 上创建 2dsphere index

db.collection.createIndex( { loc : "2dsphere" } )

这很基础,但效果很好!