Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ingesting data into MongoDB causes "stack level too deep"

I have a bunch of records stored in a mongodb collection (StudentRecord). These need to be broken out into a Collection of type Student with the Embedded Document StudentGrade. For some reason, the rake task I'm using to do this ingestion keeps hitting a "stack level too deep". I can't detect any recursive calls and I'm using bundle exec to execute it (ruby 1.9.2-p320).

task :parse_student_records => :environment do
  StudentRecord.all.each{|student_record|
    student = Student.create({:name => student_record.name})
    student.grades « Grade.create({:score => student_record.grade_score)
    student.save!
  }
end


class Student
  include MongoMapper::Document
  many :grades
  key :name, String
end

class Grade
  include MongoMapper::EmbeddedDocument
  key :grade_score, String
end

Ingesting the documents as independent documents instead of embedded documents fixes it..for some reason, the embedding causes the problem.

like image 443
udit Avatar asked May 07 '26 01:05

udit


1 Answers

That's issue 265, one of the few I know by number.

To get callbacks to fire on embedded documents in the correct order, MongoMapper has to build a huge stack that grows linearly with the number of embedded docs. At around ~600-800 docs, the stack overflows.

ActiveSupport::Callbacks is a huge mess and would require a rewrite to dodge the issue cleanly (it's less of a mess on Rails head, but still would need rewriting).

The current proposal in issue 265 is to disable the callbacks.

# in Gemfile
gem 'mongo_mapper', :git => 'git://github.com/jnunemaker/mongomapper.git', :ref => 'fefec91027f2dd8eb1ab9caa5a4b0acd000f4da7'

And then:

class Student
  include MongoMapper::Document
  embedded_callbacks_off
  # ...
end
like image 194
Brian Hempel Avatar answered May 09 '26 13:05

Brian Hempel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!