关于mongodb:mongodb-geoNear-使用记录

须要,查问一个坐标点的左近地位

数据格式

能够用数组或者geojson的模式

<field>: [ <x>, <y> ]
<field>: [<longitude>, <latitude> ]

GeoJSON

location: {
      type: "Point",
      coordinates: [-73.856077, 40.848447]
}

先列出经度,而后再列出纬度

比方 loc 字段

  {
    "city": "北京市",
    "geo_id": 565932,
    "geo_name": "万柳园(长春堂药店)",
    "lat": 39.850201868495773283,
    "lng": 116.33426020654366084,
    "loc": "[116.33426020654366084,39.850201868495773283]",
    "status": 0
 }

索引

创立 2dsphere 索引

2dsphere索引反对查问球面几何实体对象

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

pymongo查问例子

        lng = geo['lng']
        lat = geo['lat']
        result = geos_collection.aggregate([
            {"$geoNear": { 
                "near": { 
                    "type": "Point",
                    "coordinates": [lng, lat] }, 
                    "distanceField": "distance", 
                    "maxDistance": 2000, 
                    "query": {"status": -1}, 
                    "spherical": True }
            },
            {"$limit": 10}
        ])

mongodb shell

db.o2o_geos.aggregate([
   {
     $geoNear: {
        near: { type: "Point", coordinates: [120.13606048541625171, 30.29447292933346958 ] },
        distanceField: "distance",
        maxDistance: 2000,
        query: { status: -1 },
        spherical: true
     }
   }
])
  • coordinates 查问的坐标点
  • maxDistance 最大间隔
  • query 过滤条件

links

  • https://docs.mongodb.com/manu…
  • https://docs.mongodb.com/manu…
  • https://docs.mongodb.com/manu…

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据