Last active
November 5, 2018 16:12
-
-
Save digital-old-school-journey/03bc7673d88f99f05980aa6c0d66e2b7 to your computer and use it in GitHub Desktop.
Mongodb-Query-Template6-Rev02
This file contains hidden or 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.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