Skip to content

Instantly share code, notes, and snippets.

@danared
Created November 3, 2015 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danared/2f3c336c1c23ff410c07 to your computer and use it in GitHub Desktop.
Save danared/2f3c336c1c23ff410c07 to your computer and use it in GitHub Desktop.
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: 0,
Year: "_id",
highestPrice: 1,
lowestPrice: 1,
averagePrice: {$trunc: "$averagePrice"},
priceStdDev: {$trunc: "$priceStdDev"}
}
},
{
$sort:
{
"Year": -1
}
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment