Last active
December 2, 2019 15:49
-
-
Save rayrinaldy/255213a101010840140f5db32c23db20 to your computer and use it in GitHub Desktop.
Mongo Aggregate Data Grouping - Group and Sort by Date - Paginate
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
db.getCollection('videos').aggregate([{ | |
'$group': { | |
'_id': { | |
month: { | |
'$month': '$date' | |
}, | |
year: { | |
'$year': '$date' | |
}, | |
dayOfMonth: { | |
$dayOfMonth: '$date' | |
}, | |
title: '$title', | |
date: '$date', | |
file_size: '$file_size', | |
description: '$description', | |
actress: '$actress', | |
link: '$link', | |
image: '$image', | |
file_name: '$file_name', | |
tags: '$tags' | |
} | |
} | |
}, | |
{ | |
'$group': { | |
_id: { | |
month: '$_id.month', | |
year: '$_id.year', | |
dayOfMonth: '$_id.dayOfMonth' | |
}, | |
data: { | |
'$push': { | |
title: '$_id.title', | |
date: '$_id.date', | |
file_size: '$_id.file_size', | |
description: '$_id.description', | |
actress: '$_id.actress', | |
link: '$_id.link', | |
image: '$_id.image', | |
file_name: '$_id.file_name', | |
tags: '$_id.tags' | |
} | |
} | |
} | |
}, | |
{ | |
'$sort': { | |
'_id.year': 1, | |
'_id.month': 1, | |
'_id.dayOfMonth': 1, | |
} | |
}, | |
{ | |
'$facet': { | |
metadata: [{ | |
$count: "total" | |
}, { | |
$addFields: { | |
page: NumberInt(1) | |
} | |
}], | |
data: [{ | |
$skip: 0 | |
}, { | |
$limit: 10 | |
}] | |
} | |
} | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment