Skip to content

Instantly share code, notes, and snippets.

@srleo
srleo / .gitconfig
Created July 5, 2013 18:41
Useful gitconfig
[alias]
lgb = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches
@srleo
srleo / groupby.js
Last active December 19, 2015 09:49
Basic group-by count using mongodb aggregation
count_by = function(collection,field) {
return collection.aggregate({
$group: {
_id: "$" + field,
count: { $sum: 1 }
}
})
}
 
count_by(db.vid10,'char');
@srleo
srleo / gist:5688438
Last active December 17, 2015 23:19
mongodb command line function to add a 'rand' field to every document in a collection.
function add_rand_field(collection) {
collection.find().forEach(function(data) {
collection.update({_id:data._id}, {
$set:{rand:Math.random()}
});
});
collection.ensureIndex({rand:1},{background:true})
}