# File lib/mongo/collection.rb, line 464
    def update(selector, document, opts={})
      # Initial byte is 0.
      write_concern = get_write_concern(opts, self)
      message = BSON::ByteBuffer.new("\0\0\0\0")
      BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@name}")
      update_options  = 0
      update_options += 1 if opts[:upsert]
      update_options += 2 if opts[:multi]

      # Determine if update document has modifiers and check keys if so
      check_keys = document.keys.first.to_s.start_with?("$") ? false : true

      message.put_int(update_options)
      message.put_binary(BSON::BSON_CODER.serialize(selector, false, true).to_s)
      message.put_binary(BSON::BSON_CODER.serialize(document, check_keys, true, @connection.max_bson_size).to_s)

      instrument(:update, :database => @db.name, :collection => @name, :selector => selector, :document => document) do
        if Mongo::WriteConcern.gle?(write_concern)
          @connection.send_message_with_gle(Mongo::Constants::OP_UPDATE, message, @db.name, nil, write_concern)
        else
          @connection.send_message(Mongo::Constants::OP_UPDATE, message)
        end
      end
    end