I try to prepare my database field for geocoding with this:
MyCollection._ensureIndex({\'data.address.located\':\'2dsphere\'});
But then th
I got the same error, but the issue was I had a LineString
with two identical coordinates.
"geometry" : {
"type" : "LineString",
"coordinates" : [
[
143.345763,
-33.840952
],
[
143.345763,
-33.840952
]
]
}
I had to make this into a Point
for it to be valid
"geometry" : {
"type" : "Point",
"coordinates" : [
143.345763,
-33.840952
]
}
As mentioned by @go-oleg the problem is that range of coordinate are next:
and index expects coordinates to be in that bounds.
But if you are trying to apply index on already imported data and found out that coordinates are swapped, you'll probably want to swap it back rather than reimporting whole collection. For this purpose you can use next mongoshell
script.
Assuming that you already have well-formed
GeoJSON
of typePoint
.
db.coll.find().forEach(function (e) {
// assuming that `geoJson` is our field containing `coordinates`
e.geoJson.coordinates = [ // Swapping Lat/Lon
e.geoJson.coordinates[1], // Longitude goes first
e.geoJson.coordinates[0] // Latitude goes last
];
db.coll.save(e);
// Some `print(e.name)` can go here just to understand the progress
});
The problem is that
[ 32.4586858, -110.8571443 ]
is not a valid coordinate. The order should be longitude followed by latitude whereas that coordinate appears to be the reverse (judging by the fact that the valid latitude range is -90 to 90 and -110.8571443 is outside of that).
I think you meant:
{ type: "Point", coordinates: [ -110.8571443, 32.4586858 ] }
or there was some other entry error.
Even the problem was solved, I need to highlight this bug also may appear if you omit the type of GeoJSON object.
In my case, it was corrected adding the type: 'Point'
to model's value.
I had the same problem while my longitude, latitude order was correct. My mistake was that I had written coordiantes
instead of coordinates
the mongo gave me this:
pymongo.errors.WriteError: Can't extract geo keys: {data} Point must be an array or object
so maybe you have the right ordering but you have problem in your key names.
first step is to insert some data that should be in first object format otherwise this errors comes in picture
schema :
location:{
type: { type: String },
coordinates:[mongoose.Schema.Types.Mixed]
},
then insert some data into db
db.userDetails.createIndex({location: "2dsphere"})
index only be created if location contains object
after that write query ,if u want to findout distance
db.userDetails.aggregate( [
$geoNear: {
near: { type: "Point", coordinates: [data.lat1,data.lon1]},
spherical: true,
"distanceMultiplier" : 0.001,
distanceField: "calcDistance",
$maxDistance: distance,
}
] )
and one more thing data should be pass without quoated string
"lat":18.7323
"lon":73.1232
like this