geoNear: How should I setup a collection to use geoNear method on it?
D
Davide Neri
started a topic
almost 6 years ago
I haven't found examples on how to use geoNear on Kinvey, so I am making some test and I always end up having null as result.
[Kinvey reference](http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#collection-access-module "Kinvey reference") says that the parameters for the function are:
geoNear(x, y, options, callback)
[MongoDB ](http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#geonear "MongoDB reference") says the same and shows an example.
In the example some documents are added to a collection:
One thing I understood is that, in order to use geoNear, I need to add a location based index, is that right? In MongoDB documentation that's what is happening, before to create the docs, the collection index is set with:
Okkeyyyyy, the answer was easier than I thought, I have been trying for 1h before without finding it, now I found it immediately, I just had to look somewhere else.
On the [Kinvey Location guide](http://devcenter.kinvey.com/rest/guides/location "Kinvey Location guide") is written "With Kinvey all you need to do to your object is add a field with name _geoloc with a [lon,lat] value."
Davide Neri
[Kinvey reference](http://devcenter.kinvey.com/rest/reference/business-logic/reference.html#collection-access-module "Kinvey reference") says that the parameters for the function are:
geoNear(x, y, options, callback)
[MongoDB ](http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#geonear "MongoDB reference") says the same and shows an example.
In the example some documents are added to a collection:
{a:1, loc:[50, 30]}
{a:1, loc:[30, 50]}
and then geoNear is called with this syntax:
collection.geoNear(50, 50, {query:{a:1}, num:1}, function(err, docs) { ... });
On my Kinvey BL test I created a collection, with some documents like:
{_id:54e7448fd2c14b30080025f2, a:1, loc:[50, 30]}
{_id:54e7448fd2c14b30080025f3, a:1, loc:[30, 50]}
and I try to run the geoNear method in the same way:
modules.collectionAccess.collection("geotest").geoNear(50, 50, {query:{a:1}, num:1}, function(err, docs) { ... });
but as written earlier, I get a null result.
One thing I understood is that, in order to use geoNear, I need to add a location based index, is that right? In MongoDB documentation that's what is happening, before to create the docs, the collection index is set with:
collection.ensureIndex({loc:"2d"}, function(err, result) { ... });
Is that my problem? And if it is, how do I recreate it in Kinvey?