NAME

MongoDB::IndexView - Index management for a collection

VERSION

version v0.999.999.5

SYNOPSIS

my $indexes = $collection->indexes;

# listing indexes

@names = map { $_->{name} } $indexes->list->all;

my $result = $indexes->list;

while ( my $index_doc = $result->next ) {
    # do stuff with each $index_doc
}

# creating indexes

$name = $indexes->create_one( [ x => 1, y => -1 ], { unique => 1 } );

@names = $indexes->create_many(
    { keys => [ x => 1, y => -1 ], { unique => 1 } },
    { keys => [ z => 1 ] },
);

# dropping indexes

$indexes->drop_one( "x_1_y_-1" );

$indexes->drop_all;

DESCRIPTION

This class models the indexes on a MongoDB::Collection so you can create, list or drop them.

For more on MongoDB indexes, see the MongoDB Manual pages on indexing

ATTRIBUTES

collection

The MongoDB::Collection for which indexes are being created or viewed.

METHODS

list

$result = $indexes->list;

while ( my $index = $result->next ) {
    ...
}

for my $index ( $result->all ) {
    ...
}

This method returns a MongoDB::QueryResult which can be used to retrieve index information either one at a time (with next) or all at once (with all).

If the list can't be retrieved, an exception will be thrown.

create_one

$name = $indexes->create_one( [ x => 1 ] );
$name = $indexes->create_one( [ x => 1, y => 1 ] );
$name = $indexes->create_one( [ z => 1 ], { unique => 1 } );

This method takes an ordered index specification document and an optional hash reference of index options and returns the name of the index created. It will throw an exception on error.

The index specification document is an ordered document (array reference, Tie::IxHash object, or single-key hash reference) with index keys and direction/type.

See "create_many" for important information about index specifications and options.

create_many

@names = $indexes->create_many(
    { keys => [ x => 1, y => 1 ] },
    { keys => [ z => 1 ], options => { unique => 1 } }
);

This method takes a list of index models (given as hash references) and returns a list of index names created. It will throw an exception on error.

Each index module is described by the following fields:

  • keys (required) — an index specification as an ordered document (array reference, Tie::IxHash object, or single-key hash reference) with index keys and direction/type. See below for more.

  • options — an optional hash reference of index options.

The keys document needs to be ordered. You are STRONGLY encouraged to get in the habit of specifying index keys with an array reference. Because Perl randomizes the order of hash keys, you may ONLY use a hash reference if it contains a single key.

The form of the keys document differs based on the type of index (e.g. single-key, multi-key, text, geospatial, etc.).

For single and multi-key indexes, the value is "1" for an ascending index and "-1" for a descending index.

[ name => 1, votes => -1 ] # ascending on name, descending on votes

See Index Types in the MongoDB Manual for instructions for other index types.

The options hash reference may have a mix of general-purpose and index-type-specific options. See Index Options in the MongoDB Manual for specifics.

Some of the more commonly used options include:

  • background — when true, index creation won't block but will run in the background; this is strongly recommended to avoid blocking other operations on the database.

  • unique — enforce uniqueness when true; inserting a duplicate document (or creating one with update modifiers) will raise an error.

  • name — a name (string) for the index; one will be generated if this is omitted.

drop_one

$output = $indexes->drop_one( $name );

This method takes the name of an index and drops it. It returns the output of the dropIndexes command (a hash reference) on success or throws a exception if the command fails.

drop_all

$output = $indexes->drop_all;

This method drops all indexes (except the one on the _id field). It returns the output of the dropIndexes command (a hash reference) on success or throws a exception if the command fails.

AUTHORS

  • David Golden <david@mongodb.com>

  • Mike Friedman <friedo@friedo.com>

  • Kristina Chodorow <k.chodorow@gmail.com>

  • Florian Ragwitz <rafl@debian.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2015 by MongoDB, Inc..

This is free software, licensed under:

The Apache License, Version 2.0, January 2004