我一直在网上寻找解决问题的方法。我正在使用Mongoid将一组数据写入MongoDB。
我正在尝试使用mongoid进行批量插入,如下所示:
class Geonode
include Mongoid::Document
include Mongoid::Timestamps
embeds_one :location
embeds_one :transport
end
class Location
include Mongoid::Document
field :city, :type => String
embedded_in :geonode
end
class Transport
include Mongoid::Document
embeds_many :trainstation
embedded_in :geonode
end
class Trainstation
include Mongoid::Document
field :station, :type => String
belongs_to :transport
end
它可以一个接一个地完成,但是如果我想批量生产它们,我该怎么办?
我试过了。require 'moped'
require 'rubygems'
require 'mongo'
require 'mongoid'
require 'skySchema.rb' #schema is the file i defined the classes just before
Mongoid.load!("./mongoid.yml", :development)
include Mongo
batch = []
batch << {:location => Location.new(:city => "London"),
:transport => Transport.new(:trainstation =>
[Trainstation.new(:station => "Kings Cross")])}}
and then doing this many many times, after which
Geonode.collection.insert(batch)
但它不起作用。难道我做错了什么?
答案 0 :(得分:1)
问题在于,对于批量插入,您需要执行以下操作:
Geonode.insert(batch)
以不同的方式格式化批处理。现在一切都很酷。 谢谢你的帮助。