在Mongoid中更改文档的_type,可以通过以下步骤进行:
class NewClass< Mongoid::Document
include Mongoid::Document
store_in collection: 'old_collection_name'
field :new_field, type: String
end
old_document = OldClass.where(id: 'old_document_id').first
new_document = NewClass.new(old_document.attributes.except('_type'))
new_document.save!
old_document.destroy
class OldClass
include Mongoid::Document
store_in collection: 'old_collection_name'
field :old_field, type: String
def self.inherited_class
NewClass
end
end
通过以上步骤,您可以在Mongoid中更改文档的_type。
领取专属 10元无门槛券
手把手带您无忧上云