wcx4pq
Last Updated: February 25, 2016
·
5.024K
· maciejsmolinski
Picture ms

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) }
Say Thanks
Respond

2 Responses
Add your response

5635
531046 3899916467707 1900402271 n

Nice one... really useful database optimisation.

over 1 year ago ·
17260
1e9ce2c15d24684d45ae475c5d9fc4d4

Might be more efficient if you:

Topic.collection.update({},
                        { '$unset' => { type: 1 } },
                        multi: true)
over 1 year ago ·