module Mongoid::Positional

def replace_index(keys, position)

def replace_index(keys, position)
  # replace index with $ only if that key is in the selector and it is only
  # nested a single level deep.
  matches = position.scan(/\.\d+\./)
  if matches.size == 1
    keys.each do |kk|
      if position =~ /\A#{kk}\.\d+\.(.*)\z/
        return "#{kk}.$.#{$1}"
      end
    end
  end
  position
end