我的Mongo数据库中填充了邻居/地区的多边形。我需要查询Google提供的 Lng / Lat ,看看它是否在我保存的多边形之一内。如果坐标在多边形内,则返回文档。
听起来很简单,但是我花了好几个小时来处理关于SO的Mongo文档和问题,并且无法让它运行起来。有人会碰巧有一个例子让这个滚动吗?如果我的MongoDB中的数据结构不正确,我可以这样做。谢谢你的帮助!
加拿大魁北克省柯克兰(Lng / Lat)的Coords:
[-73.8667, 45.4500]
Kirkland的Polygon(来自MongoDB文档):
{
"_id" : ObjectId("5277fff17fd78c0a7c713524"),
"type" : "Polygon",
"name" : "Kirkland",
"coordinates" : "[[[-73.84639065099992, 45.46254541400003], [-73.84527964099993, 45.46145258000007], [-73.84492842099989, 45.46110707200006], [-73.84399219399995, 45.460186141000065], [-73.8407852819999, 45.45713777800006], [-73.8405644799999, 45.45693077300007], [-73.84052262699993, 45.45689154900003], [-73.84039886599993, 45.45677551000006], [-73.8390040189999, 45.45539976500004], [-73.83699086899992, 45.45341401400009], [-73.83676532899995, 45.453279922000036], [-73.83636951599993, 45.453044617000046], [-73.83427161599991, 45.45084861200007], [-73.8342537289999, 45.45083099300007], [-73.8331441099999, 45.449854249000055], [-73.83297374799992, 45.44970432200006], [-73.83352648699992, 45.44965909500007], [-73.83523820199991, 45.449519043000066], [-73.83524912799992, 45.449514374000046], [-73.83216772499992, 45.44544321700004], [-73.8355026019999, 45.44561350100008], [-73.83846350499994, 45.44558108000007], [-73.85080954599994, 45.44551408400008], [-73.85188162999992, 45.44536892000008], [-73.85520924199994, 45.44378610100006], [-73.86009216899991, 45.441481287000045], [-73.86010980599991, 45.44147187300007], [-73.86465916499992, 45.43904399200005], [-73.86704282299989, 45.44129004300004], [-73.86713420799992, 45.441376121000076], [-73.8682983409999, 45.442844182000044], [-73.8714420469999, 45.44173971100008], [-73.87279950999994, 45.44269945600007], [-73.87357053799991, 45.44242102800007], [-73.87501784999995, 45.44194323100004], [-73.8751774669999, 45.44189320100008], [-73.87618104099994, 45.44167523200008], [-73.87689832799992, 45.44151941900009], [-73.87549164599994, 45.43840867000005], [-73.87912949699995, 45.43819043500008], [-73.87997818499991, 45.43964749200006], [-73.8809716369999, 45.43921851400006], [-73.88163188499993, 45.438933389000056], [-73.8809365219999, 45.43690093300006], [-73.88340716699992, 45.43647846400006], [-73.8835718649999, 45.437520379000034], [-73.88472633599991, 45.43824372600005], [-73.88597652399994, 45.43848963100004], [-73.88672857599994, 45.43694512900004], [-73.88701594699995, 45.43690032900008], [-73.89454723199992, 45.436337964000074], [-73.89486502499994, 45.436824881000064], [-73.89504137699993, 45.43709448800007], [-73.89833881699991, 45.43618887100007], [-73.9009770429999, 45.43541548600007], [-73.90104477099992, 45.43556667800004], [-73.90143217699995, 45.43684144800005], [-73.90167934699991, 45.43765475400005], [-73.90223650199994, 45.438897293000075], [-73.90276059099995, 45.43880354600008], [-73.9038189289999, 45.44054056200008], [-73.90458511899993, 45.44097805100006], [-73.90467561799994, 45.44115400700008], [-73.90472571299995, 45.44162062300006], [-73.90475441299992, 45.441757435000056], [-73.9054071239999, 45.44486542300007], [-73.90512243199993, 45.44499702200005], [-73.90433747899993, 45.445359850000045], [-73.9045754899999, 45.446720749000065], [-73.90251337799992, 45.447330082000065], [-73.90181885899993, 45.44605801200004], [-73.90090750799993, 45.44622671400003], [-73.90116174799994, 45.44671712400009], [-73.89856674399994, 45.44746179100008], [-73.89833317199992, 45.44752881200009], [-73.89513848899992, 45.44700792800006], [-73.8950564729999, 45.44699454300007], [-73.89398644099992, 45.44715665000007], [-73.89246674599991, 45.44754371200008], [-73.8913180319999, 45.448258667000054], [-73.88994770799991, 45.44884568800006], [-73.88962110499993, 45.44901446100005], [-73.8888201539999, 45.44960221200006], [-73.88646183599991, 45.45133264100008], [-73.88641881099994, 45.45136418700008], [-73.88582581199995, 45.451799304000076], [-73.88569238199995, 45.45185328900004], [-73.88550268699993, 45.451930049000055], [-73.88356499199995, 45.452318914000045], [-73.88237926399995, 45.45250981600008], [-73.88159874099995, 45.452708312000084], [-73.8815054879999, 45.45273201200007], [-73.88051679499995, 45.45298343700006], [-73.87270614999994, 45.45706238300005], [-73.86628960799993, 45.46020504200004], [-73.8661233009999, 45.46028485000004], [-73.86623267899995, 45.46039386200004], [-73.86752501599995, 45.461574598000084], [-73.86605950199993, 45.46250052600004], [-73.86332124899991, 45.46413909600005], [-73.8610396759999, 45.465215826000076], [-73.8603632949999, 45.46596386900006], [-73.85726109799992, 45.46835262200005], [-73.8572420289999, 45.468367285000056], [-73.85658503199994, 45.46884818800004], [-73.85484660799995, 45.47030475400004], [-73.85481935299993, 45.47032760400003], [-73.85450828099994, 45.470444332000056], [-73.8533369459999, 45.46918888000005], [-73.8519519109999, 45.46794286400006], [-73.85182828899991, 45.467799712000044], [-73.85127994299995, 45.46724713600008], [-73.85125951999993, 45.467228369000054], [-73.84958285499994, 45.46568497800007], [-73.84955791599992, 45.46566045900005], [-73.84639065099992, 45.46254541400003]]]"
}
答案 0 :(得分:0)
这是一个工作示例。请注意以下与您的代码的不同之处:
名为“type”和“coordinates”的字段包含在名为“doc”的同一个超级域中。我相信这很重要。否则查询将不适合我。
db.locations.remove(); db.locations.insert({ loc : { type : "Polygon" , coordinates : [ [ [ -73.8 , 45.4 ] , [ -73.9 , 45.4 ] , [ -73.9 , 45.5 ] , [ -73.8, 45.5 ], [ -73.8 , 45.4 ] ] ] } }); db.locations.find({ loc: { $geoIntersects : { $geometry : { type : "Point", coordinates : [ -73.85 , 45.45 ] } } } }).forEach(function(entry) { printjson(entry); } )