【发布时间】:2017-08-07 04:04:00
【问题描述】:
我的架构设计:
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 内的数据 我厌倦了这个查询,但每次查询都会返回所有文档
我已经运行了 1 公里最大距离查询
db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } } )
我给了马拉松的经纬度
marathahalli 到 madiwala 的距离:- 13.4 公里
马拉松到雄伟的距离:- 20.4 公里
marathahalli 到 shanthi nagar 距离:- 23.4 公里
但是为什么我的查询返回了所有的文档。我的代码中是否有任何错误请帮助我?
我也试过了,但是出错了
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 地理查询。
-
你能帮我吗@VaibhavPatil
标签: node.js mongodb express mongoose mongoose-schema