I have a mongodb collection names locations (with restaurants data (about 10k entries)) here’s example of one entry
{
"_id" : "80880",
"locale" : "LT",
"updated_at" : "2016-02-06T23:41:59Z",
"cost" : 1,
"access" : 1,
"reverse_geocoded_address" : "Kaunas, Lithuania",
"score" : 0,
"latitude" : 54.886345,
"longitude" : 23.876619,
"open247" : 0,
"description" : ""
}
So my goal is from client send latitude and longitude and get sorted records (from nearest to farthest). So I came up with geospatial indexes.
with this code (paste into mongo console)
db.location.find().forEach(function(doc) {
bulk.find({ "_id": doc._id }).updateOne({
"$set": {
"location": {
"type": "Point",
"coordinates": [ doc.longitude, doc.latitude ]
}
}
});
counter++;
if ( counter % 1000 == 0 ) {
bulk.execute();
bulk = db.collection.initializeOrderedBulkOp();
}
})
if ( counter % 1000 != 0) {
bulk.execute();
}
and with this command db.location.createIndex({ "location": "2dsphere" })
and here’s my meteor server method
"getNearestLocations": function(lng, lat) {
var myTest = Collections.Location.find({
location: {
$near: {
$geometry: {
type: 'Point',
coordinates: [23.963850, 54.919552]
},
$maxDistance: 1000000
}
}
});
console.log(myTest);
return myTest;
}
So how can I get result from this? console.log prints [object Object] and how it is possible that this returns only ONE record!? Thanks for Your answers!