Skip to content

Instantly share code, notes, and snippets.

@danared
Created November 3, 2015 16:21
Show Gist options
  • Save danared/2f660887a01fd0b80e6f to your computer and use it in GitHub Desktop.
Save danared/2f660887a01fd0b80e6f to your computer and use it in GitHub Desktop.
db.homeSales.aggregate([
{
$sort: {amount: -1}
},
{
$group:
{
_id: {$year: "$date"},
priciestPostCode: {$first: "$address.postcode"}
}
},
{
$lookup:
{
from: "postcodes",
localField: "priciestPostCode",
foreignField: "postcode",
as: "locationData"
}
},
{
$sort: {_id: -1}
},
{
$project:
{
_id: 0,
Year: "$_id",
PostCode: "$priciestPostCode",
Location: "$locationData.location"
}
},
{
$out: "hottestLocations"
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment