问题描述
我的架构设计:
var LocationSchema = new Schema({
area: String,
loc: [
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
]
});
module.exports = mongoose.model('Location', LocationSchema);
数据已像此mongodb一样存储
Data has stored like this 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之内的数据我对此查询感到厌倦,但每次查询时间都返回所有文档
now i want to filter data within 2km or 5mi tired this query but every evry times query returns all the documents
我已经进行了1公里的最大距离查询
i have run 1km max distance query
db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } } )
我给了marathahalli纬度和经度
i given marathahalli latitude and longitude
marathahalli到madiwala的距离:-13.4公里
marathahalli to madiwala distance :- 13.4 km
马拉松哈里(Marathahalli)至雄伟的距离:-20.4公里
marathahalli to majestic distance :- 20.4 km
marathahalli到Shanthi nagar的距离:-23.4公里
marathahalli to shanthi nagar distance :- 23.4 km
但是为什么我的查询返回所有文档.我的代码中是否有任何错误,请帮帮我吗?
but why my query returns all the documnents .IS there any mistake in my code please help me out?
我也尝试过此操作,但出现错误
i tried this also but got error
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,但是我什么都没得到
i have tried in mongoshell but i am not getting anything
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>
推荐答案
在开始之前,我将添加两点-
Before starting i would add two points-
- 使用mongo时,请始终以[long.,lat.]方式插入位置
- 请指定要插入的几何类型.有关类型的更多信息,请参考mongoDB 文档.
关于您的查询,我添加了三点作为您的问题,我的数据如下所示-
Coming to your query, I added three points as your question and my data looks like below-
> 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索引.
I have given type: "Point" because this is a reserved key for using spherical type locations in mongo. Moreover I have added [long, lat] for location.Later I added a 2dSphere index on loc field.
db.test.createIndex({loc:"2dsphere"})
然后我在下面的查询中执行以了解距Marathahalli [77.6974,12.9591]-
Then I executed below query to know the distance from 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 }
以上查询仅用于测试目的,目的是了解距离.它以米为单位显示距离,因此我可以轻松地将其转换为公里.
Above query is just for testing purpose to know the distance. It tells distance in meters so i can easily convert it into kms.
现在,我确实在查询下面运行,以查找距马拉松哈里(Marathahalli)10公里以内的位置.距离,我得到了我想要的结果-
Now I did run below query to find locations from Marathahalli within 10 kms. distance and i got my desired result-
> db.test.find({ loc:{ $near:{ $geometry: {type: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}})
{ "_id" : "madiwala", "type" : "Point", "loc" : [ 77.6174, 12.9226 ] }
我发现您尝试的方法几乎没有问题-
I found few issues with approach you were trying-
- 位置必须以[long,lat]方式给出.
- 类型:使用点类型位置必须给出点".它是保留关键字.
- 距离将以米为单位.在少数几个地方,蒙地语中的弧度也是如此.请参考此 mongoDB文档以获取有关弧度距离的更多信息.
- Locations has to be given in [long, lat] manner.
- type:"Point" has to be given for using point type location. It is reserved keyword.
- Distance will be taken in meters here. At few places it is taken in radian as well in Mongo. Please refer this mongoDB doc for more info on radian distance.
希望它可以帮助您...祝您有美好的一天...:)
Hope it helps you...have a good day...:)
这篇关于mongodb筛选具有经度,纬度和特定距离的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!