MongoD...exView(3pm)

πŸ“¦
version 2.2.2 Β· 5 versions
, in debian trixie, ubuntu resolute, ubuntu stonking

✨ MongoDB::IndexView ✨

okay so this is a page from the User Contributed Perl Documentation collection and bestie it is literally about index management, it's giving very organized queen energy πŸ’…

πŸ’– NAME πŸ’–

MongoDB::IndexView β€” index management for a collection πŸŽ€


🌸 VERSION 🌸

version v2.2.2 ✨


πŸ¦‹ 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 ], options => { unique => 1 } },
    { keys => [ z => 1 ] },
);

# dropping indexes

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

$indexes->drop_all;

like look at all those vibes, listing, creating, dropping, the whole little lifecycle 🫢


πŸ‘‘ DESCRIPTION πŸ‘‘

So this class models the indexes on a MongoDB::Collection so you can create them, list them, or drop them. that's the whole main character job description, no cap πŸ’œ

For more on MongoDB indexes, see the MongoDB Manual pages on indexing at <http://docs.mongodb.org/manual/core/indexes/> πŸ“š


🧚 ATTRIBUTES 🧚

collection

The MongoDB::Collection for which indexes are being created or viewed. it's literally the collection we're doing all this for πŸ’–


⭐ 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. so like, be ready for that 😭


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. it's giving efficient, it's giving one index at a time 🌈

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. fr fr go read it πŸ‘€

The following additional options are recognized:

  • "maxTimeMS" β€” maximum time in milliseconds before the operation will time out. ⏰

create_many

@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. bulk index creating, we love a multitasker πŸ’…

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. sneaky but valid πŸ›

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. like bestie this is not a drill, ONE key only or the vibes are off πŸ’€

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 at <http://docs.mongodb.org/manual/core/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 at <http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#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. πŸ”₯
  • "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/>. 🌍
  • "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. 🏷️

Global options specified as the last value can contain the following keys:

  • "maxTimeMS" β€” maximum time in milliseconds before the operation will time out. ⏳

drop_one

$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.

okay so heads up, if the index isn't there, no exception gets thrown β€” you just get an "ok" field that's false. (and yes, the original says "will e thrown", we're keeping the typo, it's iconic in its own way 🫢)

Valid options are:

  • "maxTimeMS" β€” maximum time in milliseconds before the operation will time out. ⏰

drop_all

$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.

Valid options are:

  • "maxTimeMS" β€” maximum time in milliseconds before the operation will time out. ⏰

πŸŽ€ AUTHORS πŸŽ€

  • 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>

absolute legends, the whole lineup πŸ‘‘


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

This is free software, licensed under:

The Apache License, Version 2.0, January 2004

so yeah, that's the whole page bestie πŸ’– this was rendered by perl v5.40.1 on 2025-04-03, and the manpage itself answers to the name MongoD...exView(3pm) (truncated name and all, she's mysterious like that ✨). go forth and index responsibly, ily πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from