I am working on some project in which I have to find some users of one particular region, like greater than 20 km and less than 40 km, for which I have written query
query.location = {
$nearSphere:
{
$geometry:
{
type:'Point',
coordinates:
[
bookingData.bookingAddress.location.coordinates[0],
bookingData.bookingAddress.location.coordinates[1]
]
},
$minDistance: (20*1000),
$maxDistance: (40*1000) // this distance should be in meters (in our case I have set 40 km)
}
};
But the result doesn't show anything, even if I have the users of that latitude and longitude in my database.
When I run query
query.location = {
$nearSphere:
{
$geometry:
{
type:'Point',
coordinates:
[
bookingData.bookingAddress.location.coordinates[0],
bookingData.bookingAddress.location.coordinates[1]
]
},
$maxDistance: (40*1000) // this distance should be in meters (in our case I have set 40 km)
}
};
it shows correct result.
This is the model I have made for storing location :
bookingAddress : {
location : {
'type':{type:String,enum:CONST.GEO_JSON_TYPES.Point,default: CONST.GEO_JSON_TYPES.Point},
coordinates: {type: [Number], default: [0, 0],required: true}
},
address : {type : String,default:null}
},
Where am I going wrong?
Related
I'm using MongoDB to store about 1 million documents representing regions.
Each document contains a coordinates record in the following format
"coordinates" : {
"longitude" : -77.02687,
"latitude" : 38.888565
}
Given a set of coordinates { x, y }, what query should I run to find the region ( document ) that is closest to it?
Based on the MongoDB geospatial-queries documentation the answer is quite simple.
In order to query for locations near a region you should follow these steps
Step 1
Create an index on the location field
db.places.createIndex( { location: "2dsphere" } )
Step 2
Find regions close to { -73.9667, 40.78 } ordered by closest locations
db.places.aggregate( [
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
spherical: true,
query: { category: "Parks" },
distanceField: "calcDistance"
}
}
] )
I am not sure if there is a way to do this. I need to have a circle in mongodb and run a query against that with a box using $box to see if these two shapes overlap or not. However, Geojson does not support circles. What would be the best way to get this done?
The circle is stored like this:
places = {
...
"location": {
"type": "Point",
"coordinates": [
-79.390756,
43.706685
]
},
"radius": 100
}
I have two specific problems:
The first issue is that maxDistance is stored in the same object as the Geojson object and cannot be used in a $near query with $maxDistance; it only takes a number.
I do a partial postal code/ zip code search on Google Geocoding Api which returns a box with two corner coordinates like this:
"geometry": {
"bounds": {
"northeast": {
"lat": 43.710565,
"lng": -79.37363479999999
},
"southwest": {
"lat": 43.690848,
"lng": -79.40025399999999
}
}
As far as I know,I cannot use $box as it only works with $geoWithin.
Edit 1:
My initial plan with the circle and the box changed mainly because I did not find a suitable and efficient solution to this problem. Instead of checking if the circle overlaps with the box, now I check if a Geojson point is inside the circle as follows:
db.places.aggregate([
{"$geoNear": {near: { type: "Point", coordinates: [ -80.459293, 40.713640] },
distanceField: "dist.calculated", maxDistance: 100000,
key: 'myLocation', query: { 'SomeField': "..." }, spherical: true}},
{ "$match" : {$expr:{ $lte:['$dist.calculated', 'radius']}}}])
The problem here is that I d have to run a query within 100 KM first and then in another stage of the aggregation check the distance.
Is there a more efficient way to implement this? Thanks.
You can store a circle as point and radius. And you can use a $near query with a point and $maxDistance in meters which is the radius of the circle. See MongoDB Documentation.
Query to find all location, geometry field of the collection, at a certain distance from a point.
db.places.find(
{
location:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
$maxDistance: 5000
}
}
}
)
Query to find if a given geometry (point, polygon(rect too)) in a query intersects with a geometry of a field in the collection.
//find quests bots that matches the users location
await Collection.find({ geometry:
{ $geoIntersects:
{
{
type: "Point",
coordinates: [
-73.99460599999999,
40.7347229
]
}
}
}
});
I'm trying to achieve the following behaviour with a mongodb query:
I have documents which have a field location and a 2dshere geospatial index on that field
The documents also have a field maxdistance
The location of the user is available in the variable user.gps as a GeoJSON point
The query currently looks like this:
query["location"] = {
$nearSphere: {
$geometry: user.gps,
$maxDistance: filterDistance
}
};
This query successfully selects all the datasets in a given filterDistance relative to the user.
What i'd like to have now is "If document.maxdistance field > 0 and the distance to the user is greater than document.maxdistance" do not select the dataset.
So i have documents that should not be found if the user lives in a distance greater than a given distance saved inside the document.
I don't know hot to express this in a mongodb query and i couldn't find any example of such a query.
$near/$nearSphere don't support per-document distance. Only per query. The main reason is sorting by distance. If you need only filtering you can use $geoIntersects/$geoWithin, but you will need to change documents to contain a polygon of the covered area, like a circle Pi*2*distance around it's location.
Example:
Assuming you have a document:
{
loc: { type: "Point", coordinates: [ 10, 20 ] },
distance: 10
}
So that it is returned when user's coordinate is within the radius, e.g. [12, 25], and is not returned when user's coordinate is outside, e.g. [12,30].
For that documents should be updated to convert loc/distance pair to a polygon. For the sake of simplicity I'll use octagon, but you may want to increase number of vertices for more accurate results:
{
loc: { type: "Point", coordinates: [ 10, 20 ] },
distance: 10,
area: {
type : "Polygon",
coordinates : [[
[ 17, 27 ],
[ 10, 30 ],
[ 3, 27 ],
[ 0, 20 ],
[ 3, 13 ],
[ 10, 10 ],
[ 17, 13 ],
[ 20, 20 ],
[ 17, 27 ]
]]
}
}
Then you can use $geoIntersects to find documents which area includes user's coordinates:
db.collection.find({
area: {
$geoIntersects: {
$geometry: { type: "Point" , coordinates: [ 12, 25] }
}
}
})
Currently, we are using $centerSphere to find nearby cities. One example is that for the locality Bee Cave in Texas, USA, $centerSphere correctly found the only city Austin in a radius of 30 kilometers (As per documentation, it was converted in radians). Now, for the city Lautoka in Fiji (Lat: -17.6169618, Long: 177.4504609) it is giving the error "Spherical distance would require (unimplemented) wrapping". This is question one: what does this error mean?
We tried implementing the same by using $center. I'm aware we need to give distance in miles rather than radians. After implementing, for Bee Cave, I got US cities that were thousands or hundreds of miles away. One example is Albuquerque. I'm unable to understand why these cities are coming even after following Mongo documentation properly.
I'm using the query (for Bee Cave, TX)
db.places.find{
"geo": {
$geoWithin: { $center: [ [ -97.9524, 30.3061 ] , 18.64 ] }
}
}
$centerSphere can't handle large distances, especially if it has to wrap around the poles, see this JIRA ticket.
Regarding $geoWithin it does not accept the distance in miles, but rather the circle’s radius as measured in the units used by the coordinate system, so latitude and longitude as per the documentation. That results in a very large bounding box that does include Albuquerque
You use $near instead that allows to specify a radius in meters. For example if you use this as your test data:
db.places.insert( {
name: "Bee Cave, Texas",
location: { type: "Point", coordinates: [ -97.9524, 30.3061 ] }
} );
db.places.insert( {
name: "Austin, Texas",
location: { type: "Point", coordinates: [ -97.654724, 30.210768 ] }
} );
db.places.insert( {
name: "Albuquerque",
location: { type: "Point", coordinates: [ -106.621216, 35.113281 ] }
} );
db.places.createIndex( { location: "2dsphere" } )
You can write the following query using the factor 1609.344 to convert miles to meter
db.places.find(
{
location:
{ $near:
{
$geometry: { type: "Point", coordinates: [-97.9524, 30.3061 ] },
$maxDistance: 20*1609.344
}
}
}
)
This query returns both Bee Cave,TX and Austin, TX:
{
"_id":ObjectId("5a7190124f0cd7075d349bbc"),
"name":"Bee Cave, Texas",
"location":{
"type":"Point",
"coordinates":[
-97.9524,
30.3061
]
}
}{
"_id":ObjectId("5a7190124f0cd7075d349bbd"),
"name":"Austin, Texas",
"location":{
"type":"Point",
"coordinates":[
-97.654724,
30.210768
]
}
}
db.getCollection('places').find({
"features.geometry":{
$near: {
$geometry: {
type: "Point" ,
coordinates:[
-834502.889188578,3970333.88258796
]}
,
$maxDistance: a given number
}
}
});
Thats the code and what i want is to return documents whose geometry is near to
the [-834502.889188578,3970333.88258796]
And tha(s the error
Error: error: {
"waitedMS" : NumberLong(0),
"ok" : 0,
"errmsg" : "invalid point in geo near query $geometry argument: { type: \"Point\", coordinates: [ -834502.889188578, 3970333.88258796 ] } longitude/latitude is out of bounds, lng: -834503 lat: 3.97033e+006",
"code" : 2
}
These are not the valid lat and long values. Latitude range from +/- 90and longitude range from +/- 180.
These are not the valid lat and long values. Latitude range from +/- 90 and longitude range from +/- 180. And in mongo $geoNear coordinates first value is longitude and second value is latitude. pass as:
db.getCollection('places').find({
"features.geometry":{
$near: {
$geometry: {
type: "Point" ,
coordinates:[
longitude, latitude
]}
,
$maxDistance: a given number
}
}
});