Skip to content

Instantly share code, notes, and snippets.

@timnew
Created February 5, 2015 10:59
Show Gist options
  • Select an option

  • Save timnew/1b47f19c3ff071c9874e to your computer and use it in GitHub Desktop.

Select an option

Save timnew/1b47f19c3ff071c9874e to your computer and use it in GitHub Desktop.
Convert `$time.utc` to YYYY-MM-dd format string in MongoDB aggregation
{
$concat: [
{ '$substr': [{ '$year': '$time.utc' }, 0, 4] },
'-',
{
'$cond': [
{ '$lte': [{'$month': '$time.utc'}, 9] },
{ '$concat': ['0', { '$substr': [{ '$month': '$time.utc' }, 0, 2] } ] },
{ '$substr': [{ '$month': '$time.utc' }, 0, 2] }
]
},
'-',
{
'$cond': [
{ '$lte': [{'$dayOfMonth': '$time.utc'}, 9] },
{ '$concat': ['0', { '$substr': [{ '$dayOfMonth': '$time.utc' }, 0, 2] } ] },
{ '$substr': [{ '$dayOfMonth': '$time.utc' }, 0, 2]}]
}]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment