vibe.d beta banner
get vibe.d
0.9.0

Asynchronous I/O that doesn’t get in your way, written in D

Function MongoCollection.createIndex

Convenience method for creating a single index. Calls createIndexes

string createIndex(T) (
  T keys,
  IndexOptions indexOptions = IndexOptions.init,
  CreateIndexOptions options = CreateIndexOptions.init
) @safe
if (!is(Unqual!T == IndexModel));

string createIndex (
  const(IndexModel) keys,
  CreateIndexOptions options = CreateIndexOptions(Nullable(DontCallDestructorT(0L), true))
) @safe;

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

Parameters

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

Example

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);
}

Authors

Sönke Ludwig

Copyright

© 2012-2016 Sönke Ludwig

License

Subject to the terms of the MIT license, as written in the included LICENSE.txt file.