我似乎无法获得与 MongoDB 一起使用的边界框查询,它实际上使用了空间索引。
这是我的索引:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "config.Listings"
},
{
"v" : 2,
"key" : {
"LatLong" : "2dsphere"
},
"name" : "LatLong_2dsphere",
"ns" : "config.Listings",
"2dsphereIndexVersion" : 3
},
{
"v" : 2,
"key" : {
"ListingId" : 1
},
"name" : "ListingId",
"ns" : "config.Listings"
},
{
"v" : 2,
"key" : {
"Category" : 1,
"Status" : 1
},
"name" : "Category_and_Status",
"ns" : "config.Listings"
},
{
"v" : 2,
"key" : {
"Category" : 1,
"Status" : 1,
"Suburb" : 1
},
"name" : "Basic_Suburb_Search",
"ns" : "config.Listings"
},
{
"v" : 2,
"key" : {
"LatLong.Coordinates" : "2d"
},
"name" : "LatLong.Coordinates_2d",
"ns" : "config.Listings"
}
]
我最初只有2dsphere
索引,但后来我看到这个答案说框查询仅适用于2d
索引。
当我尝试box
查询时,它似乎没有使用任何一个索引:
db.Listings.find( {
LatLong: { $geoWithin: { $box: [ [ 144.9941034, -37.8220633 ], [ 145.0551353, -37.79494 ] ] } }
} ).explain()
它确实会返回结果,但会执行 aCOLLSCAN
并且非常慢。
我也试过这个:
db.Listings.find( {
"LatLong.coordinates": { $geoWithin: { $box: [ [ 144.9941034, -37.8220633 ], [ 145.0551353, -37.79494 ] ] } }
} )
相同的结果 - 返回数据但执行 COLLSCAN。
LatLong
是一个GeoJSON
对象。这是一个示例文档:
{
"_id" : ObjectId("5bd64f2274cb40f172a328fb"),
"LatLong" : {
"type" : "Point",
"coordinates" : [
151.2504,
-33.96767
]
},
// other props removed for simplicity
}
有人可以告诉我我做错了什么吗?