mongodb geonear

浏览:42日期:2023-06-29

问题描述

使用db.runCommand({geoNear : “collectionName” , near : [120.123456,30.654321], num : 10 })查询之前我需要准备什么样子的集合collection ?collection相应的字段和索引???

问题解答

回答1:

collection里面需要以GeoJson格式添加坐标的信息,例如:

{ type: 'Point', coordinates: [ 40, 5 ] }

还需要建一个2dsphere类型的索引,例如:

db.collection.createIndex( { <location field> : '2dsphere' } )

更多细节请看mongodb的文档:https://docs.mongodb.org/manual/core/2dsphere/

相关文章: