Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/9739571 to your computer and use it in GitHub Desktop.
Save alexbeletsky/9739571 to your computer and use it in GitHub Desktop.
aggregate.js
function aggregate(callback) {
mongo.items.aggregate([
{
$match: {created: {$gte: timespan.from, $lt: timespan.to}}
},
{
$group: {
_id: {
url: '$source',
item: '$itemId'
},
likes: { $sum: 1 }
}
},
{
$sort: {
likes: -1
}
},
{
$limit: 50
}
], callback);
}
/* 0 */
{
"0" : {
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "likeastoreproddb.items",
"name" : "_id_"
},
"1" : {
"v" : 1,
"key" : {
"itemId" : 1,
"user" : 1
},
"ns" : "likeastoreproddb.items",
"name" : "itemId_1_user_1"
},
"2" : {
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"ns" : "likeastoreproddb.items",
"name" : "ItemsTextIndex",
"weights" : {
"authorName" : 1,
"description" : 1,
"repo" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 1
},
"3" : {
"v" : 1,
"key" : {
"user" : 1
},
"ns" : "likeastoreproddb.items",
"name" : "user_1",
"background" : true,
"safe" : true
},
"4" : {
"v" : 1,
"key" : {
"user" : 1,
"type" : 1
},
"ns" : "likeastoreproddb.items",
"name" : "user_1_type_1",
"background" : true,
"safe" : true
},
"5" : {
"v" : 1,
"key" : {
"itemId" : 1,
"user" : 1,
"type" : 1
},
"ns" : "likeastoreproddb.items",
"name" : "itemId_1_user_1_type_1",
"background" : true,
"safe" : true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment