我一直在网上寻找解决我问题的方法。我正在使用 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)
但它不起作用。难道我做错了什么?