search

Home  >  Q&A  >  body text

连接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" } )
创建相应的索引。
还是无法正常工作。

巴扎黑巴扎黑2796 days ago781

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-25 09:05:38

    db.PostCo.ensureIndex( { location : "2dsphere" } )
    

    Mongodb, 2D search, I remember that the index seems to be created like this. It seems that your error is reported by the third-party package used in Java? Is it normal to query in mongodb command line?

    reply
    0
  • PHPz

    PHPz2017-04-25 09:05:38

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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-04-25 09:05:38

    It is recommended to use https://github.com/T-baby/Mon..., you can refer to the document https://t-baby.gitbooks.io/mo... to implement geographical location query, MongoDB-Plugin simplifies it Native writing method, so you can get started after reading it once

    reply
    0
  • Cancelreply