我一直在网上寻找解决我的问题的方法。我正在使用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)
但它不起作用。我做错什么了吗?
问题是,对于批量插入,您需要做:
Geonode.insert(batch)
并以不同的方式格式化批处理。现在一切都好了。谢谢大家的帮助。