class Mongoid::Criteria::Queryable::Key

Key objects represent specifications for building query expressions utilizing MongoDB selectors.

Simple key-value conditions are translated directly into expression hashes by Mongoid without utilizing Key objects. For example, the following condition:

Foo.where(price: 1)

… is translated to the following simple expression:

{price: 1}

More complex conditions would start involving Key objects. For example:

Foo.where(:price.gt => 1)

… causes a Key instance to be created thusly:

Key.new(:price, :__override__, '$gt')

This Key instance utilizes operator but not expanded nor block. The corresponding MongoDB query expression is:

{price: {'$gt' => 1}}

A yet more more complex example is the following condition:

Foo.geo_spatial(:boundary.intersects_point => [1, 10])

Processing this condition will cause a Key instance to be created as follows:

Key.new(:location, :__override__, '$geoIntersects', '$geometry') do |value|
  { "type" => POINT, "coordinates" => value }
end

… eventually producing the following MongoDB query expression:

{

boundary: {
  '$geoIntersects' => {
    '$geometry' => {
      type: "Point" ,
      coordinates: [ 1, 10 ]
    }
  }
}

}

Key instances can be thought of as procs that map a value to the MongoDB query expression required to obtain the key’s condition, given the value.