geoNear查询 near查询的升级版
geoNear查询可以看作是near查询点进化版
geoNear查询使用runCommand命令进行使用,常用使用如下:
db.runCommand({ geoNear:<collection>, near:[x,y], minDistance:(对2d索引无效,对2dsphere有效), maxDistance: num: ... })
geoNear的返回比near多了一些数据
> db.runCommand({geoNear:'location',near:[1,2],maxDistance:10,num:1})
{
"results" : [
{
"dis" : 0,
"obj" : {
"_id" : ObjectId("5b6b6fa872ff7510af7fc784"),
"w" : [
1,
2
]
}
}
],
"stats" : {
"nscanned" : 3,
"objectsLoaded" : 1,
"avgDistance" : 0,
"maxDistance" : 0,
"time" : 3396
},
"ok" : 1
}
有3个,第一个是results,表明查询到的数据,第二个是stats,指的是查询的一些参数,第三个ok为1代表查询成功。
其中stats,nscanned表明扫码了哪些数据,time是花费的时间,maxDistance最大的距离,avgDistance平均距离,
results中,dis是distance的缩写,代表查找到的数据的距离,离我们<1,2>的距离。obj是查找到的文档,如果将num限制为2,
db.runCommand({geoNear:'location',near:[1,2],maxDistance:10,num:2})
{
"results" : [
{
"dis" : 0,
"obj" : {
"_id" : ObjectId("5b6b6fa872ff7510af7fc784"),
"w" : [
1,
2
]
}
},
{
"dis" : 1,
"obj" : {
"_id" : ObjectId("5b6b6fa572ff7510af7fc783"),
"w" : [
1,
1
]
}
}
],
"stats" : {
"nscanned" : 32,
"objectsLoaded" : 2,
"avgDistance" : 0.5,
"maxDistance" : 1,
"time" : 3340
},
"ok" : 1
}
obj返回两个数据

浙公网安备 33010602011771号