Skip to content

Instantly share code, notes, and snippets.

View craigw's full-sized avatar
🦄

Craig R Webster craigw

🦄
View GitHub Profile
>> Article.enumerate_all_tags
=> {"security"=>2, "ldap"=>1, "xen"=>1, "stories"=>3, "rails"=>13, "xeriom"=>3, "mysql"=>3, ... }
>> Article.find_all_tags
=> ["agile", "ajax", "apache", "api", "caching", "coding", ... ]
>> Article.find_all_author_ids
=> ["[email protected]"]
{
"_id": "_design/articles",
"_rev": "3752119467",
"language": "javascript",
"views": {
"by_author_id_and_status_and_posted_at": {
"map": "function(doc) { if(doc.type == 'article') { emit([doc.author_id, doc.status, doc.posted_at], doc); } }"
}
// other view omitted for brevity
}
Article.find(:all, :conditions => { :author_id => "[email protected]", :status => "Live" }, :order => :posted_at)
{
"_id": "_design/articles",
"_rev": "1532981864",
"language": "javascript",
"views": {
"by_author_id_and_status": {
"map": "function(doc) { if(doc.type == 'article') { emit([doc.author_id, doc.status], doc); } }"
}
// other views cut for brevity
}
Article.find(:all, :conditions => { :author_id => "[email protected]", :status => "Live" })
// GET http://localhost:5984/blog/_view/articles/tags?group=true&group_level=1
{"rows":[
{"key":"awesome","value":1},
{"key":"agile","value":2},
{"key":"ajax","value":2},
{"key":"apache","value":2},
{"key":"api","value":1},
{"key":"caching","value":1},
{"key":"coding","value":7},
{
"tags": {
"map": "function(doc) { if(doc.type == 'article') { for(var i in doc.tags) { emit(doc.tags[i], 1); }}}",
"reduce": "function(tag, counts) { var sum = 0; for(var i = 0; i < counts.length; i++) { sum += counts[i]; }; return sum; }"
}
// other views omitted for brevity
}
function(tag, counts) {
var sum = 0;
for(var i=0; i < counts.length; i++) {
sum += counts[i];
}
return sum;
}
function(doc) {
if(doc.type == 'article') {
for(i in doc.tags) {
emit(doc.tags[i], 1);
}
}
}
{
"_id": "monkeys-are-awesome",
"_rev": "1534115156",
"type": "article",
"title": "Monkeys are awesome",
"posted_at": "2008-09-14T20:45:14Z",
"tags": [
"monkeys",
"awesome"
],