On Thu, 29 Nov 2012 07:45:54 GMT, Sönke Ludwig wrote:

On Wed, 28 Nov 2012 21:16:34 GMT, Ekyo wrote:

I noticed there's no actual support for indexes in vibe's current mongo driver. Be it related error handling or 'ensureIndex'.

Since this is a feature I need, I'll either implement it or use a different mongo driver.

Is anyone working on this feature?
Is it planned for a later milestone?

I definitely planned to do this, but haven't gotten around to reverse engineer an existing client to see what exactly needs to be sent.

Of course, it's possible to use the mongo shell to create an index (this is what I've done so far).

Indexes are a collection: system.indexes

Indexes are inserted as any other data in any other collection, for example in mongo:

db.system.indexes.insert({ "v" : 1, "key" : { "Username" : 1 }, "unique" : true, "ns" : "users.account", "name" : "Username_1" })

would add a unique index on the field "Username" for the "users.account" collection where "users" is the db name and "account" the collection

The index flags, after a look in other drivers, seems to be:

Unique = 1<<0,
DropDuplicates = 1<<2,
Background = 1<<3,
Sparse = 1<<4

Note that these flags are part of the bson to be inserted, it is a regular insert.

With vibe's current implementation, the db and collection are both in the same variable: m_collection in collection.d:52 (MongoCollection)

so we'd need to extract the db name (I suggest in the constructor)

_dbname = m_collection[0 .. indexOf('.')];

and insert a bson representing the index

...
MongoConnection.insert(_dbname ~ "system.indexes", flags, doc);


ensureIndex only queries the collection in the first place before trying to insert the index in it to avoid useless insertions.

For errors related to indexes I'll need to look further but my guess is that the errors would be in system.indexes and we only currently check for errors in the current collection.