def find_one_and_update(document, opts = {})
cmd = { :findAndModify => collection.name, :query => filter }
cmd[:update] = document
cmd[:fields] = projection if projection
cmd[:sort] = sort if sort
cmd[:new] = !!(opts[:return_document] && opts[:return_document] == :after)
cmd[:upsert] = opts[:upsert] if opts[:upsert]
cmd[:maxTimeMS] = max_time_ms if max_time_ms
cmd[:bypassDocumentValidation] = !!opts[:bypass_document_validation]
cmd[:writeConcern] = write_concern.options if write_concern
value = with_session(opts) do |session|
write_with_retry(session, write_concern) do |server, txn_num|
apply_collation!(cmd, server, opts)
apply_array_filters!(cmd, server, opts)
Operation::Command.new(
:selector => cmd,
:db_name => database.name,
:session => session,
:txn_num => txn_num
).execute(server)
end
end.first['value']
value unless value.nil? || value.empty?
end