Page 2 of 2

mongodb geospatial request

Posted: Tue Jul 08, 2014 1:10 pm
by RobertJay

Thank you very much Katya - it's called "test" and is now shared.

  1. run app and click "within miles" on footer

  2. click update

    You'll notice that e.g. the first doc returned has a latitude and longitude that is far further away from my target location (the one sent to the where param) than 1 mile

    I've sent to support my database credentials.


mongodb geospatial request

Posted: Wed Jul 09, 2014 10:20 am
by Kateryna Grynko

Hi Robert,

You seem to do everything correctly.

I would recommend that you check this thread and contact https://support.mongolab.com/home


mongodb geospatial request

Posted: Wed Jul 09, 2014 3:14 pm
by RobertJay

Thank you Katya.


mongodb geospatial request

Posted: Thu Jul 10, 2014 2:48 am
by RobertJay

UPDATE: Just wanted to report back that MongoDB support said I had set my index for the location field incorrectly - I was using the 2d version when I needed to use the 2dsphere version. All now works perfectly - and between the great support from appery and the great support from mongodb, life is good. Thanks Katya.


mongodb geospatial request

Posted: Thu Jul 10, 2014 4:55 am
by Shawn Johnson

Robert,

Do you mind sharing a screen shot, or a example of the correct solution to the issue, It would be helpful to myself and possible others

Thanks
-Shawn


mongodb geospatial request

Posted: Thu Jul 10, 2014 2:21 pm
by RobertJay

Here's what I did Shawn - hope this helps.

  1. Saved as a CSV file an Excel sheet with "latitude" and "longitude" colums.

  2. Imported (uploaded) it to MongoDB using MongoImport.exe

  3. In MongoDB shell, created a new indexed field - an object called "loc" which combines "latitude" and "longitude" using this code:

    yourdbname.yourcollectionname.find({"loc": {"$exists": 0}}).forEach(function (doc) {
    yourdbname.yourcollectionname.update({"id": doc['id']},{"$set": { "loc": [doc['longitude'], doc['latitude']]}});
    })

    yourdbname.yourcollectionname.ensureIndex({"loc":"2dsphere"})

  4. In appery.io, mapped the parameter "q" to local storage variable using this code:

    var t1 = {

    "loc":

    {"$near":
    {"type":"Point","coordinates":[target longitude -xx.xxxx ,target latitude xx.xxxxxx], "$maxDistance": target distance in miles xx}
    }

    };

    var t2 = JSON.stringify(t1);

    localStorage.setItem("local storage var name",t2);

    Note: MongoDB may have a bug (they are looking into it) where docs are returned which exceed the desired radius. But they are returned in ascending order of miles away from target location - and so you can set up a limiting request parameter "l" to restrict the number of returned docs.


mongodb geospatial request

Posted: Thu Jul 10, 2014 2:26 pm
by Shawn Johnson

Thanks Robert