Skip to content

Instantly share code, notes, and snippets.

@digital-old-school-journey
Last active November 5, 2018 16:12
Show Gist options
  • Select an option

  • Save digital-old-school-journey/03bc7673d88f99f05980aa6c0d66e2b7 to your computer and use it in GitHub Desktop.

Select an option

Save digital-old-school-journey/03bc7673d88f99f05980aa6c0d66e2b7 to your computer and use it in GitHub Desktop.
Mongodb-Query-Template6-Rev02
db.search_logs.aggregate([
{
$match: { 'keyword': 'Super car' }
},
{
"$group": {
"_id": {
'keyword': '$keyword',
'username': '$username',
'created': { $dateToString: { format: "%Y-%m-%d", date: "$created_at" } }
},
'totalCount': { $sum: 1 }
}
},
{
'$group': {
'_id':'$_id.created',
'totalCount': { $sum: '$totalCount' },
'users': {
$addToSet: { keyword:'$_id.keyword', username: '$_id.username', count: '$totalCount'}
}
}
},
// {
// $match: {
// '_id': '2018-09-17'
// }
// },
{
'$sort': {
'totalCount': -1
}
},
{
$unwind: '$users'
},
{
$sort: { 'users.count': -1 }
},
{
$limit: 3
},
{
$group:{
_id: null,
users:{
$push: { keyword:'$users.keyword', username: '$users.username', count: '$users.count'}
}
}
},
{
$project:{
_id: 0
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment