Mongo :: Collection undefined方法`insert'

时间:2015-11-24 12:48:55

标签: mongoid

我试图将数百万行从另一个数据库导入MongoDB。我的导入例程使用

MyModel.collection.insert(data_to_import)

我得到了

NoMethodError: undefined method `insert' for #<Mongo::Collection:0x000000082bb990>
/home/mika/projects/ca2/lib/tasks/data.rake:36:in `block (2 levels) in <top (required)>'
/home/mika/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
/home/mika/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'

模型用

定义
class MyModel
  include Mongoid::Document
  include Mongoid::Attributes::Dynamic

end

有没有人对正在发生的事情有任何建议?

我可以逐行保存行,但这对于数百万行来说效率很低。想让插件工作。

1 个答案:

答案 0 :(得分:5)

如果您使用的是mongoid5,请使用

MyModel.collection.insert_many(data_to_import)

如果data_to_import是数组或

MyModel.collection.insert_on(data_to_import)

如果data_import是单个文档

如果需要对数据进行验证,还可以考虑使用MyModel.create ...