class NoSE::Backend::MongoBackend::InsertStatementStep

Insert data into an index on the backend

Public Class Methods

new(client, index, fields) click to toggle source
# File lib/nose/backend/mongo.rb, line 140
def initialize(client, index, fields)
  super

  @fields = fields.map(&:id) & index.all_fields.map(&:id)
end

Public Instance Methods

process(results) click to toggle source

Insert each row into the index

# File lib/nose/backend/mongo.rb, line 147
def process(results)
  results.each do |result|
    values = Hash[@index.all_fields.map do |field|
      next unless result.key? field.id
      value = result[field.id]

      # If this is an ID, generate or construct an ObjectId
      if field.is_a?(Fields::IDField)
        value = if value.nil?
                  BSON::ObjectId.new
                else
                  BSON::ObjectId.from_string(value)
                end
      end
      [MongoBackend.field_path(@index, field).join('.'), value]
    end.compact]

    @client[@index.to_id_graph.key].update_one(
      { '_id' => values['_id'] },
      { '$set' => values },
      upsert: true
    )
  end
end