db.postcodes.aggregate([
{
$geoNear:
{
near:
{
"type": "Point",
"coordinates": [
51.5156725,
-0.727387
]},
distanceField: "distance",
num: 10000,
maxDistance: 3000,
spherical: true
}
},
{
$lookUp: {
from: "homeSales",
localField: "postcode",
foreignField: "address.postcode",
as: "priceData"
}
},
{
$unwind: "$priceData"
},
{
$group:
{
_id: {$year: "$priceData.date"},
highestPrice: {$max: "$priceData.amount"},
lowestPrice: {$min: "$priceData.amount"},
averagePrice: {$avg: "$priceData.amount"},
priceStdDev: {$stdDevPop: "$priceData.amount"}
}
},
{
$project:
{
_id: 1,
highestPrice: 1,
lowestPrice: 1,
averagePrice: {$trunc: "$averagePrice"},
priceStdDev: {$trunc: "$priceStdDev"}
}
},
{
$sort:
{
"_id": -1
}
}
]);