Skip to content

Instantly share code, notes, and snippets.

@aswad32
Last active November 8, 2015 13:25
Show Gist options
  • Save aswad32/1c36b8ca0e917e852d44 to your computer and use it in GitHub Desktop.
Save aswad32/1c36b8ca0e917e852d44 to your computer and use it in GitHub Desktop.
/*
Mongodb aggregrate function to do the following task:
1. date formatting ($project)
2. match condition
3. sort
4. limit
article schema
{
_id: ObjectId,
link: String,
title: String,
content: String
pubdate: Date
}
*/
db.articles.aggregate(
[
{
$match : { "category": "news" }
},
{
$project: {
yearMonthDay: { $dateToString: { format: "%Y-%m-%d %H:%M:%S", date: "$pubdate" } }
content: "$content",
link : "$link",
title: "$title"
}
},
{
'$sort': { '_id': -1 }
},
{
'$limit': 20
}
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment