As the title suggests i tried to add a 2dsphere index on my GeoPoints attributes on LoopbackJs. My MongoDB shell version is 3.2.3 - so it should do.
Here are my tries so far:
Adding enableGeoIndexing in my server/datasource.js
{
...
"myDs": {
"host": "localhost",
"port": 27017,
"database": "myDB",
"name": "myDs",
"connector": "mongodb",
"enableGeoIndexing": true
}
... }
Nothing seemed to change.
Adding indexes the Loopback way + having an autoupdate script:
{
"name": "NsUser",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"indexes": {
"geopoint_index": {
"geopoint": "2dsphere"
}
},
"properties": {
"created": {
"type": "date"
},
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
},
"geopoint": {
"type": "geopoint"
},
...
}
…
}
And I'm getting an error :
"ok" : 0,
"errmsg" : "Can't extract geo keys: { _id: ObjectId('5807689f01723b3ca6ba08e5'), created: new Date(1426545369000), email: \"[email protected]\", firstname: \"Louis\", lastname: \"L\", geopoint: { lat: -17.52243049, lng: -149.54396636 } } can't project geometry into spherical CRS: { lat: -17.52243049, lng: -149.54396636 }",
"code" : 16755
A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. For more information on geospatial queries, see Geospatial Queries.
a 2-sphere is an ordinary 2-dimensional sphere in 3-dimensional Euclidean space, and is the boundary of an ordinary ball (3-ball). a 3-sphere is a 3-dimensional sphere in 4-dimensional Euclidean space.
The only solution I found to add the 2dsphere index is to add a new attribute geopoint_mongo
which is an array [lng, lat]
.
Here's the script I used to convert the datas:
NsUser.createMongoGeoloc = function (cb) {
NsUser.find(function (err, users) {
var count = 0;
users.forEach(function (user) {
var change = false;
if (user.geopoint && user.geopoint.lat && user.geopoint.lng) {
count++;
user.geopoint_mongo = [user.geopoint.lng, user.geopoint.lat];
change = true;
}
if (change) {
NsUser.upsert(user);
}
});
cb(err, count);
});
};
NsUser.remoteMethod(
'createMongoGeoloc',
{
description: 'Update the Geolocation for NsUser',
returns: {arg: 'count', type: 'number'},
http: {verb: 'put'}
}
);
Then adding the mongo index did the trick:
db.NsUser.createIndex({geopoint_mongo:"2dsphere"})
But as you can see it does not really answer the question - it is just a workaround.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With