mongodb 以经度、纬度和特定距离过滤文档
mongodb filter documents with longitude,latitude and with particular distance
我的模式设计:
var LocationSchema = new Schema({
area: String,
loc: [
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
]
});
module.exports = mongoose.model('Location', LocationSchema);
数据是这样存储的mongodb
{
"_id": {
"$oid": "58c2796d734d1d46588666c6"
},
"area": "madiwla",
"loc": [
12.9226,
77.6174
]
}
{
"_id": {
"$oid": "58c27989734d1d4658866705"
},
"area": "majestic",
"loc": [
12.9767,
77.5713
]
}
{
"_id": {
"$oid": "58ca1e21734d1d2ca8566f3c"
},
"area": "shanthi nagar",
"loc": [
12.8486,
77.6837
]
}
之后我完成了
db.locations.ensureIndex({loc:"2d"})
现在我想过滤2km或5m以内的数据
我厌倦了这个查询,但每次查询 returns 所有文件
我有 运行 1km 最大距离查询
db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } } )
我给了 marathahalli 纬度和经度
marathahalli 到 madiwala 距离:- 13.4 公里
马拉泰哈利到壮丽的距离:- 20.4 公里
marathahalli 到 shanthi nagar 距离:- 23.4 公里
但是为什么我的查询 returns 所有的文档。我的代码有什么错误吗,请帮帮我?
我也试过这个但是出错了
db.locations.find({ loc : {
$near: {
$geometry: {
loc: [12.9592,77.6974]
},
$maxDistance:0,
$minDistance:250
}
} } )
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "invalid point in geo near query $geometry argument: { loc: [
12.9592, 77.6974 ] } Point must be an array or object",
"code" : 2
}
我已经在 mongoshell 中尝试过,但我什么也没得到
db.locations.createIndex({loc:"2dsphere"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 3,
"note" : "all indexes already exist",
"ok" : 1
}
rs-ds161028:PRIMARY> db.locations.aggregate([ {$geoNear: { near :{type
"Point", coordinates:[77.6974,12.9591]}, distanceField: "distance", spherical:
true } } ])
{ "_id" : ObjectId("58c27989734d1d4658866705"), "area" : "majestic", "loc" : [
2.9767, 77.5713 ], "distance" : 8017976.609884486 }
{ "_id" : ObjectId("58c2796d734d1d46588666c6"), "area" : "madiwla", "loc" : [ 1
.9226, 77.6174 ], "distance" : 8021105.860532911 }
{ "_id" : ObjectId("58ca1e21734d1d2ca8566f3c"), "area" : "shanthi nagar", "loc"
: [ 12.8486, 77.6837 ], "distance" : 8025509.538529409 }
rs-ds161028:PRIMARY> db.locations.find({ loc:{ $near:{ $geometry: {ty
e: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}})
rs-ds161028:PRIMARY>
在开始之前我要补充两点-
- 使用 mongo 时,始终以 [经度、纬度] 方式插入位置
- 请指定要插入的几何类型。有关类型的更多信息,请参阅 mongoDB documentation。
针对您的查询,我添加了三点作为您的问题,我的数据如下所示-
> db.test.find()
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] }
{ "_id" : "Majestic", "type" : "Point", "loc" : [ 77.5712, 12.9766 ] }
{ "_id" : "ShanthiNgr", "type" : "Point", "loc" : [ 77.6836, 12.8486 ] }
我给出了类型:"Point",因为这是在 mongo 中使用球形位置的保留键。此外,我还为位置添加了 [long, lat]。
后来我在 loc 字段上添加了一个 2dSphere 索引。
db.test.createIndex({loc:"2dsphere"})
然后我执行了以下查询以了解与 Marathahalli 的距离 [77.6974,12
.9591]-
> db.test.aggregate([ {$geoNear: { near :{type: "Point", coordinates:[77.6974,12
.9591]}, distanceField: "distance", spherical: true } } ])
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ], "distance": 9583.305405402776 }
{ "_id" : "ShanthiNgr", "type" : "Point", "loc" : [ 77.6836, 12.8486 ], "distance" : 12391.53898270671 }
{ "_id" : "Majestic", "type" : "Point", "loc" : [ 77.5712, 12.9766 ], "distance": 13828.057829128267 }
以上查询仅用于测试目的以了解距离。它以米为单位显示距离,因此我可以轻松地将其转换为公里。
现在,我执行了 运行 下面的查询,以查找 10 公里以内的 Marathahalli 的位置。距离,我得到了我想要的结果-
> db.test.find({ loc:{ $near:{ $geometry: {type: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}})
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] }
我发现您尝试的方法几乎没有问题-
- 位置必须以 [long, lat] 方式给出。
- type:"Point" 必须给出使用点类型位置。是保留关键字。
- 此处距离以米为单位。在少数地方,它也以弧度表示 Mongo。请参阅 this mongoDB 文档以获取有关弧度距离的更多信息。
希望对您有所帮助...祝您有愉快的一天...:)
我的模式设计:
var LocationSchema = new Schema({
area: String,
loc: [
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
]
});
module.exports = mongoose.model('Location', LocationSchema);
数据是这样存储的mongodb
{
"_id": {
"$oid": "58c2796d734d1d46588666c6"
},
"area": "madiwla",
"loc": [
12.9226,
77.6174
]
}
{
"_id": {
"$oid": "58c27989734d1d4658866705"
},
"area": "majestic",
"loc": [
12.9767,
77.5713
]
}
{
"_id": {
"$oid": "58ca1e21734d1d2ca8566f3c"
},
"area": "shanthi nagar",
"loc": [
12.8486,
77.6837
]
}
之后我完成了
db.locations.ensureIndex({loc:"2d"})
现在我想过滤2km或5m以内的数据 我厌倦了这个查询,但每次查询 returns 所有文件
我有 运行 1km 最大距离查询
db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } } )
我给了 marathahalli 纬度和经度
marathahalli 到 madiwala 距离:- 13.4 公里
马拉泰哈利到壮丽的距离:- 20.4 公里
marathahalli 到 shanthi nagar 距离:- 23.4 公里
但是为什么我的查询 returns 所有的文档。我的代码有什么错误吗,请帮帮我?
我也试过这个但是出错了
db.locations.find({ loc : {
$near: {
$geometry: {
loc: [12.9592,77.6974]
},
$maxDistance:0,
$minDistance:250
}
} } )
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "invalid point in geo near query $geometry argument: { loc: [
12.9592, 77.6974 ] } Point must be an array or object",
"code" : 2
}
我已经在 mongoshell 中尝试过,但我什么也没得到
db.locations.createIndex({loc:"2dsphere"})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 3,
"numIndexesAfter" : 3,
"note" : "all indexes already exist",
"ok" : 1
}
rs-ds161028:PRIMARY> db.locations.aggregate([ {$geoNear: { near :{type
"Point", coordinates:[77.6974,12.9591]}, distanceField: "distance", spherical:
true } } ])
{ "_id" : ObjectId("58c27989734d1d4658866705"), "area" : "majestic", "loc" : [
2.9767, 77.5713 ], "distance" : 8017976.609884486 }
{ "_id" : ObjectId("58c2796d734d1d46588666c6"), "area" : "madiwla", "loc" : [ 1
.9226, 77.6174 ], "distance" : 8021105.860532911 }
{ "_id" : ObjectId("58ca1e21734d1d2ca8566f3c"), "area" : "shanthi nagar", "loc"
: [ 12.8486, 77.6837 ], "distance" : 8025509.538529409 }
rs-ds161028:PRIMARY> db.locations.find({ loc:{ $near:{ $geometry: {ty
e: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}})
rs-ds161028:PRIMARY>
在开始之前我要补充两点-
- 使用 mongo 时,始终以 [经度、纬度] 方式插入位置
- 请指定要插入的几何类型。有关类型的更多信息,请参阅 mongoDB documentation。
针对您的查询,我添加了三点作为您的问题,我的数据如下所示-
> db.test.find()
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] }
{ "_id" : "Majestic", "type" : "Point", "loc" : [ 77.5712, 12.9766 ] }
{ "_id" : "ShanthiNgr", "type" : "Point", "loc" : [ 77.6836, 12.8486 ] }
我给出了类型:"Point",因为这是在 mongo 中使用球形位置的保留键。此外,我还为位置添加了 [long, lat]。 后来我在 loc 字段上添加了一个 2dSphere 索引。
db.test.createIndex({loc:"2dsphere"})
然后我执行了以下查询以了解与 Marathahalli 的距离 [77.6974,12 .9591]-
> db.test.aggregate([ {$geoNear: { near :{type: "Point", coordinates:[77.6974,12
.9591]}, distanceField: "distance", spherical: true } } ])
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ], "distance": 9583.305405402776 }
{ "_id" : "ShanthiNgr", "type" : "Point", "loc" : [ 77.6836, 12.8486 ], "distance" : 12391.53898270671 }
{ "_id" : "Majestic", "type" : "Point", "loc" : [ 77.5712, 12.9766 ], "distance": 13828.057829128267 }
以上查询仅用于测试目的以了解距离。它以米为单位显示距离,因此我可以轻松地将其转换为公里。
现在,我执行了 运行 下面的查询,以查找 10 公里以内的 Marathahalli 的位置。距离,我得到了我想要的结果-
> db.test.find({ loc:{ $near:{ $geometry: {type: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}})
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] }
我发现您尝试的方法几乎没有问题-
- 位置必须以 [long, lat] 方式给出。
- type:"Point" 必须给出使用点类型位置。是保留关键字。
- 此处距离以米为单位。在少数地方,它也以弧度表示 Mongo。请参阅 this mongoDB 文档以获取有关弧度距离的更多信息。
希望对您有所帮助...祝您有愉快的一天...:)