Last active
November 8, 2015 13:25
-
-
Save aswad32/1c36b8ca0e917e852d44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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