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(), true))
) @safe;
Supports any kind of document for template parameter T or a IndexModel.
Parameters
Name | Description |
---|---|
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);
}