stijnsanders/TMongoWire — pull request #8 by compiled (2012-05-01 13:42:32+00:00)
Title: Added ensureIndex

Hi,

I needed to create indexes from my application so I implemented the ensureIndex method.

Kind regards,

Fred Oranje

## Usage

Wire.ensureIndex('database', 'collection', BSON(['name', 1]), BSON(['unique', true]));

## Choices

### Options as IBSONDocument

Allows you to specify the options which you need and keeps the databases defaults values when you don't specify them.

### Name generation

The [MongoDB wiki](http://www.mongodb.org/display/DOCS/Indexes) contains the following text about creating indexes.

> name is also an option but need not be specified and will be deprecated in the future. The name of an index is generated by concatenating the names of the indexed fields and their direction (i.e., 1 or -1 for ascending or descending). Index names (including their namespace/database), are limited to 128 characters.

Initially I tried adding an index without specifying the name, but the index wouldn't insert. So I guess the text about the deprecated name option is written for the Mongo Shell. I added index name generation based on the results of the shell and I also check for the maximum length (including namespace/database) as described above.

I also made the choice to be able to specify your own name in case you want a different index name, or the generated one it too long.