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] }
Related
I have field location array for coordinates
{
location: [13.3339, 80.1943],
...someOtherfield
}
can I use $near in mongoose with that data? specifically in mongoose aggregate?
or I must change data field like this
{
location: {
type: { type: 'Point' },
coordinates: [13.3339, 80.1943],
},
}
Your document implies that you are storing location using Legacy Coordinate Pairs
location: [<longitude>, <latitude> ]
Yes, you can use $near with your data, if you have 2d index, on your location field. 2d index
After that, You can query like this
db.collection.find(
{ location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
)
You can refer it from here
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
}
}
}
)
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
}
}
});
i want to find near by location so inserting record like this..
db.locationcol.insert({"location":"phase 8,mohali ,punjab ,india","service":"psychologist","loc":{"lon":76.703347,"lat":30.710459}})
and then executing Query on terminal .
db.runCommand(
{
geoNear: "locationcol",
near: { type: "Point", coordinates: [ 76.720845, 30.712097 ] },
spherical: true,
query: { category: "public" }
})
but it is returning ..
{ "ok" : 0, "errmsg" : "no geo indices for geoNear" }
i am also trying it with Spring ...
public GeoResults getnearby(double longitude,double latitude, String service) {
Point point = new Point(longitude,latitude);
Query query = new Query(Criteria.where("service").is(service));
query.fields().include("service").include("location").include("loc");
NearQuery nearQuery = NearQuery.near(point).maxDistance(new Distance(50, Metrics.KILOMETERS));
nearQuery.query(query);
nearQuery.num(20);
GeoResults<locationcol> data = operations.geoNear(nearQuery, locationcol.class,"locationcol");
return data;
}
this code is returning empty list .i am not getting that where i am going wrong. help !!
Before you can execute geospatial queries, you need to create a geospatial index:
db.locationcol.createIndex( { loc : "2dsphere" } )
Also, you need to store your locations as valid GeoJSON objects so MongoDB can parse them properly:
loc : { type: "Point", coordinates: [ -76.703347, 30.710459 ] },
I am trying to query for data points within a given lat/lng range. Can you reference the elements of an object in a mongoose query like I have done ('location.lat') and ('location.long')? If so, I am not getting any data back from this query. I was querying for all data points, and that was working just fine - now I am simply trying to refine the query to only the points in a given range.
EDIT
Querying for points that have a new format (see the updated schema below):
var range = {"topLeft":[-113.51526849999999,53.24204911518776],"bottomRight":[-131.0933935,41.397215826886736],"topRight":[-131.0933935,53.24204911518776],"bottomLeft":[-113.51526849999999,41.397215826886736]};
db.datapoints.find({
geo: {
$geoWithin : {
$geometry: {
type: "Polygon",
coordinates: [
[
range.topLeft, range.topRight, range.bottomRight, range.bottomLeft
]
]
}
}
}
})
but I am getting:
error: {
"$err" : "Malformed geo query: { $geoWithin: { $geometry: { type: \"Polygon\", coordinates: [ [ [ -113.5152685, 53.24204911518776 ], [ -131.0933935, 53.24204911518776 ], [ -131.0933935, 41.39721582688674 ], [ -113.5152685, 41.39721582688674 ] ] ] } } }",
"code" : 16677
}
NOTE: the range parameter looks like this:
UPDATED SCHEMA
var mongoose = require('mongoose');
var dataPointSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
geo: {type: [Number], index: '2d'},
...
timestamp: {type: Date, default: Date.now}
...
});
As seen here:
MongoDB: "Malformed geo query" with $geoIntersect on a polygon
You must close the polygon by making the first and last points the same.