我的架构设计:
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"})
现在我想在2公里或5米范围内过滤数据 我厌倦了这个查询,但每次evry次查询都返回所有文件
我跑了1km最大距离查询
db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } } )
我给了marathahalli纬度和经度
marathahalli到madiwala距离: - 13.4 km
marathahalli到雄伟的距离: - 20.4公里
marathahalli到shanthi nagar距离: - 23.4 km
但为什么我的查询会返回所有documnents。我的代码中有任何错误请帮助我吗?
我也尝试了这个但是出错了
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>
答案 0 :(得分:2)
在开始之前我会加两点 -
来到您的查询,我添加了三点作为您的问题,我的数据如下所示 -
> 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 0.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 ] }
我发现你尝试的方法几乎没有问题 -
希望它能帮助你......祝你有个美好的一天...... :)。