Mongo::Collection undefined method `insert'

走远了吗. 提交于 2019-12-12 06:23:56

问题


I'm trying to import millions of rows from another database into MongoDB. My routine for import uses

MyModel.collection.insert(data_to_import)

And I get

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>'

The model is defined with

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

end

Does anyone have any suggestions about what is happening?

I can save the rows one by one but that is so inefficient for millions of rows. Would like to get the insert to work.


回答1:


If you are using mongoid5 use

MyModel.collection.insert_many(data_to_import)

if data_to_import is an array or

MyModel.collection.insert_on(data_to_import)

if data_import is a single document

also consider using MyModel.create if you need valdating the data...



来源:https://stackoverflow.com/questions/33894132/mongocollection-undefined-method-insert

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!