2013-06-05 4 views
1

나는 웹상에서 내 문제에 대한 해결책을 찾고 있었다. MongoDB를 사용하여 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) 

그리고 다른 방법으로 배치를 포맷 :

+0

"작동하지 않는다"는 의미는 무엇입니까? 너 오류가있어? 첫 번째 것만 삽입됩니까? 다른 것? –

+0

개체가 삽입되지 않고 충돌합니다. 그리고 다른 것을 정의해야하는지 궁금하네요? 아무런 문제가 발생하지 않습니다. .rvm/gems/ruby-1.9.3-p429/gems/mongoid-3.1.4/lib/mongoid/attributes.rb : 320 :'method_missing '에서 : 정의되지 않은 메소드 # (NoMethodError)에 대한 '__bson_dump__' – inquire

+0

Mongoid의 버전은 무엇입니까? MongoDB 드라이버가 배치 삽입 구문을 항상 지원하지는 않았으므로 이전 버전의 Ruby 드라이버 버전을 기반으로 할 수 있습니까? –

답변

1

문제는 일괄 삽입, 당신이 할 필요가 있다고했다. 모두 멋지다. 도움 주셔서 감사합니다.

+1

어떻게 작동하도록 일괄 처리를 포맷해야 했습니까? – Rafal

+0

@Rafal까지 내가했던 기억으로 ' 배치 같은 것을 = [] 항목 = {// 여기에 물건} 배치 << 항목 ' 다음 'YourEntity.create (배치) ' 또는 'YourEntity.insert (배치)' 그래서 당신은 배열의 모든 항목을 추가 한 다음 물건의 배열을 삽입하고는 하드웨어에 따라 일괄 적으로 분할하고이를 삽입합니다. 내 인서트 속도는 한 번에 약 80 이었지만 얼마 전이었고 꽤 나쁜 하드웨어를 사용하고있었습니다. 도움이되는지 알려주세요. – inquire

+1

고마워, 실제로 배치와 함께 작동하도록 임베디드 클래스에서 as_document를 호출해야했다. 배치 << Object.new ({: a => 1, : sub_obj => SubObj.new ({: b => 2}). as_document})). as_document – Rafal