Skip to content

Instantly share code, notes, and snippets.

@cam-gists
Created October 1, 2012 01:46
Show Gist options
  • Save cam-gists/3809003 to your computer and use it in GitHub Desktop.
Save cam-gists/3809003 to your computer and use it in GitHub Desktop.
MongoDB: Aggregate Array Values
// ref: http://stackoverflow.com/questions/12654451/twitter-data-finding-the-most-mentioned-user-in-mongodb
// ref: http://stackoverflow.com/questions/12665866/mongodb-aggregating-array-items/12666156#comment17092191_12666156
var count = 0;
db.COLLECTION.find({key: "value"},{key_to_count:1}).forEach(
function (doc) {
count += doc.key_to_count.length;
}
)
print("key_to_count: " + count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment