简体   繁体   English

如何在MongoDB中正确使用$ geoIntersects?

[英]How to use $geoIntersects in MongoDB properly?

I wann to use $geoIntersects for checking whether a point is in a polygon. 我想使用$ geoIntersects来检查点是否在多边形中。

I have saved UK regions from here in my mongo database. 我从这里将 Mongo数据库中保存了英国地区。 I saved all geoJSON entity with mongoose-geojson-schema module. 我用mongoose-geojson-schema模块保存了所有geoJSON实体。

Here is defined schema in mongoose 这是在猫鼬中定义的架构

var geozoneSchema = mongoose.model('Geozone', {
    geoFeature:  GeoJSON.Feature,
    average:    {type: Number, default: null},
    updated_at: {type: Date, default: Date.now}
});

I wrote this query, but it didn't work. 我写了这个查询,但是没有用。

Geozone.findOne({
    "geometry.coordinates": {
        "$geoIntersects": {
            "$geometry": {
                "type": "Point",
                "coordinates": fakeUser.loc
            }
        }
    }
}, function(err, foundGeozone) {
    if (err) throw err;
    console.log('found geozone: ', foundGeozone);
    if (foundGeozone) {
        console.log('found geozone! Good! Save User.');
    }
});

In fakeUser.loc is stored this point 这一点存储在fakeUser.loc中

 user.loc = [-3.2, 54.5]; //real loc

So I am sure that point really inside particular stored region, but variable foundGeozone is null. 因此,我可以确定该点确实在特定的存储区域内,但是变量foundGeozone为null。

Maybe, my query is wrong or something else. 也许,我的查询有误或其他原因。 Could you help me? 你可以帮帮我吗?

From the data schema in https://github.com/glynnbird/ukcountiesgeojson , your location field to search should be " geometry ", not " geometry.coordinates ": https://github.com/glynnbird/ukcountiesgeojson中的数据模式中,您要搜索的位置字段应为“ geometry ”,而不是“ geometry.coordinates ”:

Geozone.findOne({
    "geometry": {
        "$geoIntersects": {
            "$geometry": {
                "type": "Point",
                "coordinates": fakeUser.loc
            }
        }
    }
}, function(err, foundGeozone) {
    if (err) throw err;
    console.log('found geozone: ', foundGeozone);
    if (foundGeozone) {
        console.log('found geozone! Good! Save User.');
    }
});

In Victor's case, the location field should be 'geoFeature.geometry' 在维克多的情况下,位置字段应为“ geoFeature.geometry”

Geozone.findOne({ 'geoFeature.geometry':  
    {$geoIntersects:
      {$geometry:{ "type" : "Point",
        "coordinates" : [longitude, latitude] } //fakeUser.loc
      }
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM