Mongodb 复合 2dsphere 索引无法按预期工作
Posted
技术标签:
【中文标题】Mongodb 复合 2dsphere 索引无法按预期工作【英文标题】:Mongodb compound 2dsphere index dosen't work as expected 【发布时间】:2013-10-06 07:12:18 【问题描述】:我有一个名为 search2 的集合,其中包含大约 20000 个这样的文档:
"loc":
"type": "Polygon",
"coordinates": [
[
[
43.78526674007639,
11.14739998758569
],
[
43.78526674007639,
11.183372851822439
],
[
43.79443488391605,
11.183372851822439
],
[
43.79443488391605,
11.264311796355125
],
[
43.812771171595415,
11.264311796355125
],
[
43.83110745927479,
11.264311796355125
],
[
43.83110745927479,
11.273305012414314
],
[
43.849443746954144,
11.273305012414314
],
[
43.858611890793824,
11.273305012414314
],
[
43.858611890793824,
11.264311796355125
],
[
43.8769481784732,
11.264311796355125
],
[
43.8769481784732,
11.246325364236752
],
[
43.88611632231286,
11.246325364236752
],
[
43.88611632231286,
11.237332148177565
],
[
43.895284466152546,
11.237332148177565
],
[
43.895284466152546,
11.228338932118376
],
[
43.904452609992234,
11.228338932118376
],
[
43.904452609992234,
11.165386419704065
],
[
43.895284466152546,
11.165386419704065
],
[
43.895284466152546,
11.156393203644878
],
[
43.88611632231286,
11.156393203644878
],
[
43.8769481784732,
11.156393203644878
],
[
43.858611890793824,
11.156393203644878
],
[
43.849443746954144,
11.156393203644878
],
[
43.849443746954144,
11.165386419704065
],
[
43.83110745927479,
11.165386419704065
],
[
43.83110745927479,
11.156393203644878
],
[
43.812771171595415,
11.156393203644878
],
[
43.812771171595415,
11.14739998758569
],
[
43.79443488391605,
11.14739998758569
],
[
43.78526674007639,
11.14739998758569
]
]
]
,
"docId": 1,
"docVote": 0,
"title": "title-1",
"_id":
"$oid": "5248725d2dd5622510000001"
我用这个命令定义了一个索引:
db.search2.ensureIndex("docVote": 1,"loc":"2dsphere");
集合上只有这个索引和“_id”字段的默认索引。
当我执行以下查询时,我希望“nscannedObjects”为 = 10:
db.search2.find(
loc:
$geoIntersects:
$geometry:
type: "Polygon",
coordinates: [
[
[43.7269795, 11.1540365],
[43.8329368, 11.1540365],
[43.8329368, 11.3310908],
[43.7269795, 11.3310908],
[43.7269795, 11.1540365]
]
]
,
"docVote": 1,
_id: 0
).sort(
"docVote": 1
).limit(10).hint(
"docVote": 1,
"loc": "2dsphere"
).explain()
但结果是这样的:
"cursor" : "S2Cursor",
"isMultiKey" : true,
"n" : 10,
"nscannedObjects" : 44283,
"nscanned" : 648117,
"nscannedObjectsAllPlans" : 44283,
"nscannedAllPlans" : 648117,
"scanAndOrder" : true,
"indexOnly" : false,
"nYields" : 13,
"nChunkSkips" : 0,
"millis" : 12632,
"indexBounds" :
,
"nscanned" : 648117,
"matchTested" : NumberLong(46642),
"geoTested" : NumberLong(46642),
"cellsInCover" : NumberLong(8),
"server" : "*********"
如果我从查询中删除排序,我会得到:
"cursor" : "S2Cursor",
"isMultiKey" : true,
"n" : 10,
"nscannedObjects" : 10,
"nscanned" : 25,
"nscannedObjectsAllPlans" : 10,
"nscannedAllPlans" : 25,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 3,
"indexBounds" :
,
"nscanned" : 25,
"matchTested" : NumberLong(10),
"geoTested" : NumberLong(10),
"cellsInCover" : NumberLong(8),
"server" : "******"
那么,为什么不使用索引来对结果进行排序呢? 从这个文档: http://docs.mongodb.org/manual/applications/geospatial-indexes/ http://docs.mongodb.org/manual/tutorial/sort-results-with-indexes/ 我了解 MongoDb 支持“以标量索引字段(即升序或降序)作为 2dsphere 索引字段的前缀或后缀的复合索引”以及“如果排序文档是复合索引的子集并且从索引,MongoDB 可以使用索引来检索和排序查询结果。”
我错过了什么?
提前致谢
【问题讨论】:
db.searchtest2path.ensureIndex("docVote": 1,"loc":"2dsphere");关于样本的集合和关于索引的声明是不一样的。可能是一个错字,我会更好地解决你的问题:) 谢谢。我已经解决了这个问题! 【参考方案1】:我的想法是因为您将 docVote 定义为复合索引中的第一个字段,但您没有在查询中使用 docVote。
我的建议是尝试 db.search2.ensureIndex("loc":"2dsphere");查看查询是否使用索引。 然后尝试 db.search2.ensureIndex("loc":"2dsphere", "docVote": 1);
【讨论】:
【参考方案2】:删除“loc”上的索引并仅尝试使用“docVote”索引。
【讨论】:
嗨牧师,谢谢你的回答。我已经尝试过了,但它不起作用,因为这样不使用 2dsphere 索引。以上是关于Mongodb 复合 2dsphere 索引无法按预期工作的主要内容,如果未能解决你的问题,请参考以下文章
使用 2dsphere 索引和 IP 地址索引索引 mongodb 集合
MongoDB 创建具有用于空间查询的特征数组的 2dsphere 索引