1

我一直在网上寻找解决我问题的方法。我正在使用 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)

但它不起作用。难道我做错了什么?

4

1 回答 1

1

问题是对于批量插入,您需要执行以下操作:

Geonode.insert(batch)

并以不同的方式格式化批处理。现在一切都很酷。谢谢你们的帮助。

于 2013-07-09T10:48:36.617 回答