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
}
}
});
Related
i noticed that mongoDB $near and $geoNear returns around 1km bigger distance than should be.
Point1 (current location):
lat: 54.8985,
long: 23.9036,
Point2 (location in DB):
"location" : {
"type" : "Point",
"coordinates" : [
54.91689,
23.97423
]
}
based on multiple libraries distance should be around 4.7KM
however mongodb returns: "calcDistance" : 8082 = 8km
DIFFERENCE is 4 KM!
You can check it here:
I have tried both spherical and non-spehrical and tried both $geowNear and $near
my code:
db.getCollection('collection').aggregate([
{
$geoNear: {
near: { type: 'Point', coordinates: [ 54.8985, 23.9036 ] },
distanceField: 'calcDistance',
maxDistance: 5 * 1000,
}
}
])
Any ideas why?
Look slike $near and $geoNear need lat and long swapped. That was a problem.
Refer to this: geoNear returns incorrect distance
Say I have a collection with entries like so:
{_id: "foo" lng: -98.21 lat: 77.131 }
{_id: "foo" lng: -100.12 lat: 770.313 }
{_id: "foo" lng: -98.32 lat: 772.123 }
where lng and lat correspond to the longitude and latitude.
I want to return only the entries that are within a certain radius of the the input longitude and latitude. So that would mean that for radius R, sqrt((inputLatiude - docLatitude)^2 + (inputLongitude - docLonguitude)^2) < R.
I don't want to return all the documents with an added field that tells me whether it's within R or not. I want to return only the fields that match the condition.
How do I query this?
You'll want to use $near here to do so. However $near requires a 2dsphere index built which currently seems like you don't have one as the document structure does not match the required structure.
So what do you have to do?
Restructure your document so the lng and lat fields could be 2dsphere indexed:
{
_id: "foo",
loc : { type: "Point", coordinates: [ -98.21, 77.131 ] },
}
Build such index on the field loc.
Use $near to query:
db.collection.find(
{
loc:
{ $near :
{
$geometry: { type: "Point", coordinates: [ inputLng, inputLat ] },
$maxDistance: R
}
}
}
)
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?
I have a model like the following:
var locSchema = new Schema({
loc:{
type:[Number],
index: '2dsphere'
}
});
And, I'm using $geoNear command like this:
Location.aggregate([{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [parseInt(req.query.longitude), parseInt(req.query.latitude)]
},
"spherical": true,
"distanceField": "distance"
}
});
I'm uploading the longitude and latitude from a location and saving it in the model. And, I'm retrieving the models using the same location in geoNear command. That is same longitude and latitude.
But, in the result, I have distance as a big float where I should get 0 (or at least near 0).
{
_id: "578ca6613525d44b3f5171b8",
loc: [
79.9811093,
14.450403300000001
],
distance: 117139.297675591
}
I tried the distanceMultiplier but that does not seem to work. Where am I mistaken?
I'm trying to add an 2dSphere index to a Mongo Collection that contains an array of locations. This is the error I'm getting:
[MongoError: insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?:
{ _id: "gjuFiwHd7ctBpgEwL",
title: "Iny orple taoland.",
locations: [
{ loc:
{ type: "Point", coordinates: [ "0.0181033", "43.8355792" ] }
}
]
}
]
Where I'm calling:
ensureIndex({ 'locations.loc': "2dsphere" })
What am I doing wrong?
For 2dsphere, points are specified in GeoJSON format. A point is give by a two-element array, representing[longitude,latitude]. You should change your coordinates array.
The lon and lat points shouldn't be wrapped in double qoutes.
It should be:
{ type: "Point", coordinates: [ 0.0181033, 43.8355792] }