MongoDB::IndexView - Index management for a collection
version v2.1.0
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 ], options => { unique => 1 } }, { keys => [ z => 1 ] }, ); # dropping indexes $indexes->drop_one( "x_1_y_-1" ); $indexes->drop_all;
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
The MongoDB::Collection for which indexes are being created or viewed.
$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).
next
all
If the list can't be retrieved, an exception will be thrown.
$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.
The following additional options are recognized:
maxTimeMS — maximum time in milliseconds before the operation will time out.
maxTimeMS
@names = $indexes->create_many( { keys => [ x => 1, y => 1 ] }, { keys => [ z => 1 ], options => { unique => 1 } } ); @names = $indexes->create_many( { keys => [ x => 1, y => 1 ] }, { keys => [ z => 1 ], options => { unique => 1 } } \%global_options, );
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.
If the last value is a hash reference without a keys entry, it will be assumed to be a set of global options. See below for a list of accepted global options.
keys
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.
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.
background
collation - a document defining the collation for this operation. See docs for the format of the collation document here: https://docs.mongodb.com/master/reference/collation/.
collation
unique — enforce uniqueness when true; inserting a duplicate document (or creating one with update modifiers) will raise an error.
unique
name — a name (string) for the index; one will be generated if this is omitted.
name
Global options specified as the last value can contain the following keys:
$output = $indexes->drop_one( $name ); $output = $indexes->drop_one( $name, \%options );
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 errors. However, if the index does not exist, the command output will have the ok field as a false value, but no exception will e thrown.
ok
Valid options are:
$output = $indexes->drop_all; $output = $indexes->drop_all(\%options);
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.
_id
David Golden <david@mongodb.com>
Rassi <rassi@mongodb.com>
Mike Friedman <friedo@friedo.com>
Kristina Chodorow <k.chodorow@gmail.com>
Florian Ragwitz <rafl@debian.org>
This software is Copyright (c) 2019 by MongoDB, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
To install MongoDB, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoDB
CPAN shell
perl -MCPAN -e shell install MongoDB
For more information on module installation, please visit the detailed CPAN module installation guide.