how to sort by proximity and date in mongoDB? I tried this. But they just sort by date:
coll.find({\'date\':{$gte:date},\'location\':{$nearSphere:[lat,lng]}})
It's recommended to use $geoNear in an aggregate : https://docs.mongodb.com/manual/reference/operator/aggregation/geoNear/
You can sort on your date and distance in the aggregate :
coll.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ lat, lng ] },
key: "location",
spherical: true,
distanceField: "dist.calculated",
query: { "date": {"$gte": date} }
}
},
{$sort: {"dist.calculated":1, "date": 1}}
])