Mika
Mika

Reputation: 1479

Mongo::Collection undefined method `insert'

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.

Upvotes: 1

Views: 1787

Answers (1)

kuadrosx
kuadrosx

Reputation: 411

If you are using mongoid5 use

MyModel.collection.insert_many(data_to_import)

if data_to_import is an array or

MyModel.collection.insert_one(data_to_import)

if data_import is a single document

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

Upvotes: 8

Related Questions