0
votes

Hi when I try to do continue on error for insert many it doesnt work in mongoid.

I set the following unique index

db.push_notifications.createIndex({ actor_vid: 1,campaign_id: 1 },{ unique: true, partialFilterExpression: { campaign_id: { $exists: true } } })

PushNotification.collection.insert_many([{:campaign_id => "1",:actor_vid => 9},{:campaign_id => "1",:actor_vid => 8}],:continue_on_error => true, :safe => false)

PushNotification.collection.insert_many([{:campaign_id => "1",:actor_vid => 9},{:campaign_id => "1",:actor_vid => 10}],:continue_on_error => true, :safe => false) throws

Mongo::Error::BulkWriteError: Mongo::Error::BulkWriteError from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/bulk_write/result.rb:184:in validate!' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/bulk_write/result_combiner.rb:73:inresult' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/bulk_write.rb:65:in execute' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection.rb:385:inbulk_write' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/mongo-2.2.5/lib/mongo/collection.rb:363:in insert_many' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/mongoid-5.1.3/lib/mongoid/query_cache.rb:168:ininsert_many_with_clear_cache' from (irb):133 from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in start' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:instart' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in console' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:inrun_command!' from /home/deploy/.bundler/notification_service/ruby/2.2.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in <top (required)>' from script/rails:6:inrequire' from script/rails:6:in `'

OR What is the mongo equivalent for mysql insert ignore? I need to perform insert_many operation with bypassing error on unique keys

1

1 Answers

6
votes

continue_on_error is not an option for insert_many , use unordered inserts instead.By specifying ordered: false , inserts will happen in an unordered fashion and it will try to insert all requests.Including an try catch block will make sure it won't break after an exception, so you are achieving an MYSQL INSERT IGNORE equivalent. If your using ROR, this is how your code should be,

begin
 PushNotification.collection.insert_many([{:campaign_id => "1",:actor_vid => 10},{:campaign_id => "1",:actor_vid => 11},{:campaign_id => "1",:actor_vid => 12}],{:ordered => false}) 
 PushNotification.collection.insert_many([{:campaign_id => "1",:actor_vid => 10},{:campaign_id => "1",:actor_vid => 11},{:campaign_id => "1",:actor_vid => 13}],{:ordered => false}) 
resque => ex 
 puts ex.message 
end

So, after the block is executed , you will have 4 new entries inserted and 2 Mongo::Error::BulkWriteError Exception.