MongoCollection.createIndex

Convenience method for creating a single index. Calls createIndexes

Supports any kind of document for template parameter T or a IndexModel.

  1. string createIndex(T keys, IndexOptions indexOptions, CreateIndexOptions options)
  2. string createIndex(IndexModel keys, CreateIndexOptions options)
    struct MongoCollection
    @safe
    string
    createIndex
    (
    const IndexModel keys
    ,
    CreateIndexOptions options = CreateIndexOptions.init
    )

Parameters

keys IndexModel

a IndexModel or type with integer or string fields indicating index direction or index type.

Examples

import vibe.db.mongo.mongo;

void test()
{
	auto coll = connectMongoDB("127.0.0.1").getCollection("test");

	// simple ascending name, descending primarykey compound-index
	coll.createIndex(["name": 1, "primarykey": -1]);

	IndexOptions textOptions = {
		// pick language from another field called "idioma"
		languageOverride: "idioma"
	};
	auto textIndex = IndexModel()
			.withOptions(textOptions)
			.add("comments", IndexType.text);
	// more complex text index in DB with independent language
	coll.createIndex(textIndex);
}

Meta