簡體   English   中英

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

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

我想使用$ geoIntersects來檢查點是否在多邊形中。

我從這里將 Mongo數據庫中保存了英國地區。 我用mongoose-geojson-schema模塊保存了所有geoJSON實體。

這是在貓鼬中定義的架構

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

我寫了這個查詢,但是沒有用。

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.');
    }
});

這一點存儲在fakeUser.loc中

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

因此,我可以確定該點確實在特定的存儲區域內,但是變量foundGeozone為null。

也許,我的查詢有誤或其他原因。 你可以幫幫我嗎?

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.');
    }
});

在維克多的情況下,位置字段應為“ 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