$ geowithin:$ polygon不使用2dsphere索引 [英] $geowithin:$polygon doesn't use 2dsphere index
问题描述
我正在收集如下文件:
{
"_id" : ObjectId("5bc15f23d672e9086ca4fbac"),
"Location" : {
"GeoJson" : {
"type" : "Point",
"coordinates" : [14.4199254356, 50.0700249768]
}
}
我已经按照以下步骤创建了索引
And I have Index created as following
{ Location.GeoJson2dsphere : "2dsphere" }
现在当我使用$polygon
进行搜索时出现了问题,我得到了结果,但是查询未使用索引,因此速度很慢.这是查询
And now to the problem when I use the $polygon
for searching, i've got the results, but query is not using index, so it's slow. Here is the query
.find({"Location.GeoJson" : {
"$geoWithin" : {
"$polygon" : [
[14.4182910543168, 50.0891393887804],
[14.4491901021683, 50.0891393887804],
[14.4491901021683, 50.0671069088523],
[14.4182910543168, 50.0671069088523]
]
}
}
})
但是当我改用$ Geometry时,它使用的是索引.
But when I use $Geometry instead, it is using index.
.find({"Location.GeoJson" : {
"$geoWithin" :
{"$geometry" :
{"type" : "Polygon",
"coordinates" : [[
[14.4182910543168, 50.0891393887804],
[14.4491901021683, 50.0891393887804],
[14.4491901021683, 50.0671069088523],
[14.4182910543168, 50.0671069088523],
[14.4182910543168, 50.0891393887804]
]]
}}
}})
是否有任何原因导致第一个查询不使用索引? Mongo手册对此没有说什么. 您能指出我,使用$ polygon搜索索引该怎么做,还是我需要重写我应用程序中的所有查询才能使用$ geometry.我正在使用C#驱动程序,其语法如下所示:
Is there any reason why the first query isn't using the index? Mongo manual is not saying anything about this. Can you point me, what to do to use $polygon for searching index, or do I need to rewrite all the queries in my app, to use $geometry. I am using C# driver where the syntax looks like this:
Builders<Offer>.Filter.GeoWithinPolygon(a => a.Location.GeoJson, polygon);
但是,这将产生第一个不使用索引的查询.
However, this is producingf the first query, which is not using index.
推荐答案
我不为mongo工作,所以不会冒险猜测为什么(除了旧数据模型还是新数据模型)令新手感到困惑,但这是如何 mongo如何区分 infinite-flat-2d ($多边形查询)索引和 spherical 索引($ geometry查询)
I don't work for mongo, so won't hazard a guess as to why (beyond legacy vs new data models) being confusing for newcomers, but here is how mongo differentiates between an infinite-flat-2d ($polygon query) index and a spherical index ($geometry query)
传递 $ polygon 会针对旧版2d索引,而 $ geometry 会针对2d球形索引. Mongo将这些查询分为两种不同的对象类型
passing a $polygon targets a legacy 2d index, where or a $geometry targets the 2d-spherical index. Mongo splits out these queries to two different object types
此处: https ://github.com/mongodb/mongo/blob/master/src/mongo/db/geo/geoparser.cpp#L785-L790
Mongo确实说只能通过 $ geometry 查询(不是传统的 $ polygon 查询)只能访问2dsphere索引
Mongo does say that the 2dsphere index is only accessable though a $geometry query (not the legacy $polygon query)
此处: https://docs.mongodb.com/手册/教程/query-a-2dsphere-index/
如您所述: Mongo超级有帮助,即使您的查询不匹配,它仍然可以返回数据.
As you noted: Mongo is super helpful in that it can still return data, even if you've mismatched your queries.
为什么 $ polygon 甚至不再是东西?
Why is $polygon even a thing anymore?
对于那些希望在无限的平面上使用2d索引的人,Mongo继续支持非球面2d索引.也许是用于视频游戏,或者用于无限平坦的平面足以避免浮点数学运算(以及相关的舍入问题)的任何用途
Mongo continues to support non-spherical 2d indexes for those that want a 2d index on an infinite flat surface. Perhaps for video games or for any use where an infinite flat surface is a good enough approximation to avoid floating point math (and associated rounding questions)
在此处描述: https://docs.mongodb.com /manual/tutorial/query-a-2d-index/
这篇关于$ geowithin:$ polygon不使用2dsphere索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!