Can you use MongoDB map/reduce to migrate data?
- by Brian Armstrong
I have a large collection where I want to modify all the documents by populating a field.
A simple example might be caching the comment count on each post:
class Post
field :comment_count, type: Integer
has_many :comments
end
class Comment
belongs_to :post
end
I can run it in serial with something like:
Post.all.each do |p|
p.udpate_attribute :comment_count, p.comments.count
end
But it's taking 24 hours to run (large collection). I was wondering if mongo's map/reduce could be used for this? But I haven't seen a great example yet.
I imagine you would map off the comments collection and then store the reduced results in the posts collection. Am I on the right track?