Skip to content

Instantly share code, notes, and snippets.

@lemonlatte
Created April 20, 2020 03:53
Show Gist options
  • Save lemonlatte/0f76941703b3d19610b3911e03f050c5 to your computer and use it in GitHub Desktop.
Save lemonlatte/0f76941703b3d19610b3911e03f050c5 to your computer and use it in GitHub Desktop.
Mongo Aggregation example
db.profile.aggregate([
{
"$match": {
"account_number" : "e4CHPviKRu5P6L5YQ15qYL77tfXEGua4U4maPTmzf4YwxCMA9d"
}
},
{
$unwind: "$points_of_interest"
},
{
"$addFields": {
"points_of_interest.order": {
"$switch": {
"branches": [
{ "case": { "$eq": ["$points_of_interest.id", ObjectId("5e97cd49e8d6887bdc22b9ed")] }, then: 1 },
{ "case": { "$eq": ["$points_of_interest.id", ObjectId("5e97cd9ce8d6887bdc22b9ef")] }, then: 0 },
{ "case": { "$eq": ["$points_of_interest.id", ObjectId("5e97cda8e8d6887bdc22b9f0")] }, then: 2 }
]
}
}
}
},
{
"$sort": {
"points_of_interest.order": 1
},
},
{
$group: {
"_id": "$_id",
"points_of_interest": {
"$push": "$points_of_interest"
}
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment