Mongo :: Collection未定义方法`insert' [英] Mongo::Collection undefined method `insert'

查看:105
本文介绍了Mongo :: Collection未定义方法`insert'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数百万行的数据从另一个数据库导入MongoDB.我的日常进口用途

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

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

有人对发生的事情有任何建议吗?

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.

推荐答案

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

If you are using mongoid5 use

MyModel.collection.insert_many(data_to_import)

如果data_to_import是数组或

if data_to_import is an array or

MyModel.collection.insert_one(data_to_import)

如果data_import是单个文档

if data_import is a single document

如果需要评估数据,还可以考虑使用MyModel.create ...

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

这篇关于Mongo :: Collection未定义方法`insert'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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