Created
December 7, 2011 21:46
-
-
Save neovintage/1444829 to your computer and use it in GitHub Desktop.
MongoDB tips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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