问题
My schema design:
var LocationSchema = new Schema({
area: String,
loc: [
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
]
});
module.exports = mongoose.model('Location', LocationSchema);
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
]
}
after that i have done
db.locations.ensureIndex({loc:"2d"})
now i want to filter data within 2km or 5m i tired this query but every evry times query returns all the documents
i have run 1km max distance query
db.locations.find({ loc : { $near : [12.9592,77.6974] , $maxDistance : 100/111.12 } } )
i given marathahalli latitude and longitude
marathahalli to madiwala distance :- 13.4 km
marathahalli to majestic distance :- 20.4 km
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
}
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>
回答1:
Before starting i would add two points-
- When using mongo, always insert locations in [long., lat.] manner
- Please specify type of geometry you are inserting. Refer mongoDB documentation for more info on types.
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 ] }
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"})
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.
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-
- 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...:)
来源:https://stackoverflow.com/questions/42826320/mongodb-filter-documents-with-longitude-latitude-and-with-particular-distance