Mongoid 8 slow insertions?

Hello!

We have similar issue with mongoid 8. We have a test to insert 1000 documents using factory_bot. With mongoid 7.5.4 it takes about 9 seconds. With mongoid 8.1.4 it takes 780 seconds (13 minutes). We observe, that each new document insertion takes more time than previous one.

Please, find our sample code below:

class First
  include Mongoid::Document
  include Mongoid::Timestamps::Short

  field :name, type: String

  has_many :seconds, :dependent => :destroy
  has_many :thirds, dependent: :destroy
  
  validates_uniqueness_of :name
  validates_presence_of :name

end

class Second
  include Mongoid::Document
  include Mongoid::Timestamps::Short

  field :name, type: String

  belongs_to :first
  has_and_belongs_to_many :thirds
end

class Third
  include Mongoid::Document
  include Mongoid::Timestamps::Short

  field :name, type: String

  validates_presence_of :name
  validates_format_of :name, without: /\/|\\|\&|\?|\s/
  
  has_and_belongs_to_many :seconds
  belongs_to :first
end

Test factories for factory_bot:

FactoryBot.define do
  factory :first do
    sequence(:name){ |i| "First name_#{i}" }
  end
end

FactoryBot.define do
  factory :second do
    sequence(:name){ |i| "Second name_#{i}" }
  end
 end

FactoryBot.define do
  factory :third do
    sequence(:name){ |i| "Third name_#{i}" }
  end
end

And test:

let(:first) { create(:first) }
let(:second_1) { create(:second, first: first) }
let(:second_2) { create(:second, first: first) }
let(:thirds_1000) { create_list(:third, 1000, first: first, seconds: [second_1, second_2]) }

it 'checks 1000 thirds insertion' do
  start_time = Time.now.utc
  thirds_1000
  puts "Time spent: #{Time.now.utc - start_time} seconds"  
end

Output for mongoid 7.5.4:
Time spent: 8.92853051 seconds

Output for mongoid 8.1.4:
Time spent: 780.104147085 seconds

Best Regards,
Ilya Starovoytov