Skip to content

Instantly share code, notes, and snippets.

@neovintage
Created December 7, 2011 21:46
Show Gist options
  • Save neovintage/1444829 to your computer and use it in GitHub Desktop.
Save neovintage/1444829 to your computer and use it in GitHub Desktop.
MongoDB tips
class Post
include Mongoid::Document
field :author
field :text
field :publish_date, :type => Date
embeds_many :comments
index [:author, :asc]
end
class Comment
include Mongoid::Document
field :author
field :text
field :publish_date, :type => Date
embedded_in :post, :inverse_of => :comments
index [:publish_date, :desc]
end
class Post
include Mongoid::Document
field :author
field :text
field :publish_date, :type => Date
embeds_many :comments
index [:author, :asc]
index ["comments.publish_date", :desc]
end
class Comment
include Mongoid::Document
field :author
field :text
field :publish_date, :type => Date
embedded_in :post, :inverse_of => :comments
end
m = 'function(){ this.tags.forEach( function(z){ emit( z , { count : 1 } ); } ); };'
r = <<-REDUCE
function( key , values ){
var total = 0;
for ( var i=0; i < values.length; i++ )
total += values[i].count;
return { count : total };
};
REDUCE
Post.collection.map_reduce(m, r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment