Can you use MongoDB map/reduce to migrate data?
        Posted  
        
            by 
                Brian Armstrong
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Brian Armstrong
        
        
        
        Published on 2012-05-30T22:53:28Z
        Indexed on 
            2012/06/03
            4:40 UTC
        
        
        Read the original article
        Hit count: 251
        
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?
© Stack Overflow or respective owner