使用mongodb/mongoid更改运行时模型 [英] Runtime changing model with mongodb/mongoid

查看:52
本文介绍了使用mongodb/mongoid更改运行时模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在mongoid模型中添加几个字段,我知道不存在MongoDB的迁移,但是如果我继续进行操作而没有删除数据库,则使Rails完全重新生成"了数据库,则它不会显示或使用完全是新领域!

I've to add several fields in a mongoid model, I know there is not migration with MongoDB but if I go on without dropping the DB, making rails to "regenerate" the DB entirely, it doesn't display or use the new fields at all !

去这里最好的方法是什么?有什么比删除/重新打开mongodb软的东西吗?

What's the best way to go here ? Is there something softer than drop/reopen mongodb ?

先谢谢了 卢卡

推荐答案

通常,应该可以在运行时使用新字段更新旧文档.无需在MongoDB中进行迁移.

In general it should be possible to update old documents with the new fields at runtime. There is no need for migrations in MongoDB.

您可能想编写rake任务,以使用新字段和默认值更新旧文档.

You maybe want to write rake tasks to update your old documents with the new fields and default values.

您可以通过检查默认情况下为零的那些新字段来查找这些文档.

You could find out these documents by checking those new fields which have per default a nil value.

更新

简单样式:

Easy style:

如果您使用默认值定义一个新字段,则只要您设置一个新字段,就应始终使用该值:

If you define a new field with a default value, this value should always be used as long as you set a new one:

app/models/my_model.rb

app/models/my_model.rb

class MyModel
  include Mongoid::Document
  field :name, type: String
  field :data, type: String
  # NEW FIELD
  field :note, type: String, default: "no note given so far!"
end

如果您查询数据库,则应获取扩展名前没有此字段的文档的默认值:

If you query your database you should get your default value for documents which haven't this field before your extension:

(rails控制台)

MyModel.first
#=> #<MyModel …other fields…, note: "no note given so far!">

我在Ruby 1.9.2上使用带有当前Mongoid的新鲜rails堆栈进行了测试-应该也可以与其他堆栈一起使用.

I tested this with a fresh rails stack with a current mongoid on Ruby 1.9.2 - should work with other stacks, too.

更复杂的样式:

More complicated/complex style:

如果您未设置默认值,则此新字段将为nil.

If you didn't set a default value, you'll get nil for this new field.

app/models/my_model.rb

app/models/my_model.rb

class MyModel
  include Mongoid::Document
  field :name, type: String
  field :data, type: String
  # NEW FIELD
  field :note, type: String
end

(rails控制台)

MyModel.first
#=> #<MyModel …other fields…, note: nil>

然后,您可以按照以下示例设置rake任务和迁移文件:

Then you could set up a rake task and migration file like in this example:

lib/tasks/my_model_migration.rake:

lib/tasks/my_model_migration.rake:

namespace :mymodel do
  desc "MyModel migration task"
  task :migrate => :environment do
    require "./db/migrate.rb"
  end
end

db/migrate.rb:

db/migrate.rb:

olds = MyModel.where(note: nil)
# Enumerator of documents without a valid :note field (= nil)
olds.each do |doc|
  doc.note = "(migration) no note given yet"
  # or whatever your desired default value should be
  doc.save! rescue puts "Could not modify doc #{doc.id}/#{doc.name}"
  # the rescue is only a failsafe statement if something goes wrong
end

使用rake mymodel:migrate运行此迁移.

这只是一个起点,您可以将其扩展到完整的蒙古式迁移引擎.

This is only a starting point and you can extend this to a full mongoid migration engine.

task :migrate => :environment do …是必需的,否则rake不会加载模型.

The task :migrate => :environment do … is necessary, otherwise rake won't load models.

这篇关于使用mongodb/mongoid更改运行时模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆