Add a Mongo GeoJSON 2dsphere index on LoopbackJs' attribute GeoPoint

indexing, loopbackjs, mongodb

Solution

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.

Problem

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: \"xxx@gmail.com\", 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 ```

Original source