teemu
1
Is the near by location search possible?
Is it possible to use mongodb $geoNear function to find documents. Strapi support 2dsphere index out of the box but how to make query?
I have tried multiple ways but I lack of skills 
const result = strapi.query("place").model.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [61.49231574416126, 21.800464597811054],
},
distanceField: "dist.distance",
includeLocs: "dist.location",
$maxDistance: 150000,
spherical: true,
},
},
]);
teemu
2
Solution so far
const result = strapi.query("place").model.find({
location: {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [61.492315, 21.800464],
},
distanceField: "dist.distance",
includeLocs: "dist.location",
$maxDistance: 10000,
spherical: true,
},
},
});
const fields = result.map((entry) => entry.toObject());
console.log(fields) gives:
Query {
_mongooseOptions: {},
_transforms: [ [Function (anonymous)] ],
_hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} },
_executionCount: 0,
mongooseCollection: NativeCollection {
collection: Collection { s: [Object] },
Promise: [Function: Promise],
_closed: false,
opts: {
schemaUserProvidedOptions: [Object],
capped: false,
autoCreate: undefined,
Promise: [Function: Promise],
'$wasForceClosed': undefined
},
name: 'places',
collectionName: 'places',
...
teemu
3
Okay so following return places within some range. Some params are not accepted (commented off)
const query = strapi.query("place").model.find({
geometry: {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [61.49231574416126, 21.800464597811054],
},
$maxDistance: 300,
// distanceField: "dist.distance",
// includeLocs: "dist.location",
// spherical: true,
},
},
});
query.exec(function (err, places) {
if (err) console.log(err);
if (places) console.log(places);
});