mongoid/mongoid — Issue #1361: Ability to properly construct primary keys
Author: y8
Created: 2011-10-20 16:24:51+00:00
State: open

Since mongodb support complex primary keys, there should be a way to define structure of primary key.

This will be useful, when you don't need a ObjectID primary key, and you have fields which will be used as index, and will make unique combination.

For example:

````ruby
class Measure
field :measured_at, type: DateTime
field :instrument_id, type: Integer
field :value, type: Integer
field :quality, type: Integer

# Since measured_at and instrument_id will make unique combination, we may make it primary key
# to reduce collection and indexes size
key :measured_at, :instrument_id
end
`````

With this notation, mongoid should start storing `measured_at` and `instrument_id` fields inside mongo `_id` attribute, and accordingly change queries like `Measure.where(:instrument_id => @thermo.id)` to search for `instrument_id` into `_id`.

There two options: to change key behavior to described or create new class method.