场景:根据坐标来查询附近一定距离的单车
数据结构如下:
{
"_id" : ObjectId("5a8ac5ea2fec3e02c4d7e0e0"),//自动生成
"bike_no" : "2000003", //单车编号
"location" : { //文档要求这样写的,type类型可以换,coordinates代表坐标
"type" : "Point",
"coordinates" : [
113.685102,
34.802361
]
},
"status" : 1 //单车是否可用
}
查询附近距离的单车sql 死套路,记住就行
db.getCollection('bike-position').find({
location: {
$nearSphere: {
type: "Point",
coordinates: [113.684824, 34.803043]
},
$maxDistance: 100
}
, status: 1
})