stijnsanders / TMongoWire

Delphi MongoDB driver
MIT License
102 stars 37 forks source link

Added ensureIndex #8

Closed smokingdev closed 12 years ago

smokingdev commented 12 years ago

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 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.

stijnsanders commented 12 years ago

I'm thinking of letting BSON() accept IBSONDocument values where a key is expected, to make it 'clone' the data into the resulting IBSONDocument, but it's quite some work so for now you're right in 'transporting' the Options into the Document.

Also, would it be a good idea to use a UUID as name when the length exceeds 128? If would it be better to raise like you do now?

I'm also thinking about something separate to contain all database administration calls, a class or unit or something, I haven't made up my mind yet,but I'll merge this for the time being.

smokingdev commented 12 years ago

Sorry, for the late response.

I choose to raise an exception so that the user knows default behaviour could not be followed and he has the chance to set his own name. Since the name of the index won't change based on the user data (right?), it is determined at development time, so the exception gets noticed by the developer and he has the opportunity to provide his own index name. That way the developer allows knows which index has what name.

evandroabreu commented 12 years ago

Hello, I'm having the following error: [Error] mongoWire.pas (531): Undeclared identifier: 'mongoWire_Db_SystemIndexCollection'. I'm using Delphi 7.

this error occurs when trying to run the demo that comes with the driver source code.

How can I solve this problem?

stijnsanders commented 12 years ago

You're right. Odd, looks like this got dropped from the merge, I guess I did something wrong. I've added it, please check the latest version of the main repo.