jnunemaker/mongomapper — pull request #353 by spinosa (2011-11-23 14:56:39+00:00)
Title: Key abbreviation

Thanks for taking the time to look over the first pull request. Appreciate and agree with the advice.

The key option is now :abbr and I've overrode keys#to_mongo instead of keys#attributes(true/false).

Thoughts?

--

...because Mongo stores the entire key with each document, and that's terrible with large datasets.

This patch allows you to add the :abbr option to any key which will define what string is actually stored in Mongo as the key. This is done transparently, so your code can continue to use the human-readable long key while persisting in mongo with the (presumably shorter) abbreviation.

For example:

```ruby
class User
include MongoMapper::Document
key :email_address, String, :abbr => :e
end

u = User.find_by_email_address("spinosa@gmail.com")
u.email_address = "dan@shelby.tv"
u.save

#user in db looks like {_id: 'NormalObjectId', e: 'dan@shelby.tv' }
```