Mongoid: Remove unnecessary properties from documents
Assuming we've got such a model:
# -*- encoding : utf-8 -*-
class Topic
  include Mongoid::Document
  field :name, type: String
  # No Longer Supported Property:
  # field :type, type: Integer
end
And we'd like to clear all documents from no longer supported property (:type), in Mongoid it's as simple as running:
Topic.each { |topic| topic.unset(:type) }Written by Maciej Smoliński
Related protips
2 Responses
Nice one... really useful database optimisation.
over 1 year ago
·
Might be more efficient if you:
Topic.collection.update({},
                        { '$unset' => { type: 1 } },
                        multi: true)
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
 #Ruby 
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#