连接mongodb,查询地理位置的问题
巴扎黑
巴扎黑 2017-04-25 09:03:38
[MongoDB讨论组]

com.mongodb.MongoException: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { position: { $nearSphere: [ 120.344944, 36.067463 ] } }
at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:214)
at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198)
at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176)
at com.mongodb.QueryResultIterator.(QueryResultIterator.java:64)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:86)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
at com.mongodb.DBCursor._check(DBCursor.java:458)
at com.mongodb.DBCursor._hasNext(DBCursor.java:546)
at com.mongodb.DBCursor.hasNext(DBCursor.java:571)

我已经利用
db.PostCo.createIndex( { location : "2dsphere" } )
db.PostCo.createIndex( { location : "2d" } )
创建相应的索引。
还是无法正常工作。

巴扎黑
巴扎黑

全部回复(3)
黄舟
db.PostCo.ensureIndex( { location : "2dsphere" } )

mongodb,2d搜索,我记得好像是这么创建索引的,看你的错误是在java中用的第三方包报错了?在mongodb命令行中查询正常吗?

PHPz

db.PostCo.ensureIndex({"location":"2d"},{min:0,max:200})试试。

给我你的怀抱

推荐使用https://github.com/T-baby/Mon...,可以参考文档https://t-baby.gitbooks.io/mo... 来实现地理位置查询,MongoDB-Plugin简化了原生写法所以看一遍就可以上手了

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号