1MongoDB::IndexView(3) User Contributed Perl DocumentationMongoDB::IndexView(3)
2
3
4
6 MongoDB::IndexView - Index management for a collection
7
9 version v2.2.2
10
12 my $indexes = $collection->indexes;
13
14 # listing indexes
15
16 @names = map { $_->{name} } $indexes->list->all;
17
18 my $result = $indexes->list;
19
20 while ( my $index_doc = $result->next ) {
21 # do stuff with each $index_doc
22 }
23
24 # creating indexes
25
26 $name = $indexes->create_one( [ x => 1, y => -1 ], { unique => 1 } );
27
28 @names = $indexes->create_many(
29 { keys => [ x => 1, y => -1 ], options => { unique => 1 } },
30 { keys => [ z => 1 ] },
31 );
32
33 # dropping indexes
34
35 $indexes->drop_one( "x_1_y_-1" );
36
37 $indexes->drop_all;
38
40 This class models the indexes on a MongoDB::Collection so you can
41 create, list or drop them.
42
43 For more on MongoDB indexes, see the MongoDB Manual pages on indexing
44 <http://docs.mongodb.org/manual/core/indexes/>
45
47 collection
48 The MongoDB::Collection for which indexes are being created or viewed.
49
51 list
52 $result = $indexes->list;
53
54 while ( my $index = $result->next ) {
55 ...
56 }
57
58 for my $index ( $result->all ) {
59 ...
60 }
61
62 This method returns a MongoDB::QueryResult which can be used to
63 retrieve index information either one at a time (with "next") or all at
64 once (with "all").
65
66 If the list can't be retrieved, an exception will be thrown.
67
68 create_one
69 $name = $indexes->create_one( [ x => 1 ] );
70 $name = $indexes->create_one( [ x => 1, y => 1 ] );
71 $name = $indexes->create_one( [ z => 1 ], { unique => 1 } );
72
73 This method takes an ordered index specification document and an
74 optional hash reference of index options and returns the name of the
75 index created. It will throw an exception on error.
76
77 The index specification document is an ordered document (array
78 reference, Tie::IxHash object, or single-key hash reference) with index
79 keys and direction/type.
80
81 See "create_many" for important information about index specifications
82 and options.
83
84 The following additional options are recognized:
85
86 • "maxTimeMS" — maximum time in milliseconds before the operation
87 will time out.
88
89 create_many
90 @names = $indexes->create_many(
91 { keys => [ x => 1, y => 1 ] },
92 { keys => [ z => 1 ], options => { unique => 1 } }
93 );
94
95 @names = $indexes->create_many(
96 { keys => [ x => 1, y => 1 ] },
97 { keys => [ z => 1 ], options => { unique => 1 } }
98 \%global_options,
99 );
100
101 This method takes a list of index models (given as hash references) and
102 returns a list of index names created. It will throw an exception on
103 error.
104
105 If the last value is a hash reference without a "keys" entry, it will
106 be assumed to be a set of global options. See below for a list of
107 accepted global options.
108
109 Each index module is described by the following fields:
110
111 • "keys" (required) — an index specification as an ordered document
112 (array reference, Tie::IxHash object, or single-key hash reference)
113 with index keys and direction/type. See below for more.
114
115 • "options" — an optional hash reference of index options.
116
117 The "keys" document needs to be ordered. You are STRONGLY encouraged
118 to get in the habit of specifying index keys with an array reference.
119 Because Perl randomizes the order of hash keys, you may ONLY use a hash
120 reference if it contains a single key.
121
122 The form of the "keys" document differs based on the type of index
123 (e.g. single-key, multi-key, text, geospatial, etc.).
124
125 For single and multi-key indexes, the value is "1" for an ascending
126 index and "-1" for a descending index.
127
128 [ name => 1, votes => -1 ] # ascending on name, descending on votes
129
130 See Index Types <http://docs.mongodb.org/manual/core/index-types/> in
131 the MongoDB Manual for instructions for other index types.
132
133 The "options" hash reference may have a mix of general-purpose and
134 index-type-specific options. See Index Options
135 <http://docs.mongodb.org/manual/reference/method/db.collection.createIndex/#options>
136 in the MongoDB Manual for specifics.
137
138 Some of the more commonly used options include:
139
140 • "background" — when true, index creation won't block but will run
141 in the background; this is strongly recommended to avoid blocking
142 other operations on the database.
143
144 • "collation" - a document defining the collation for this operation.
145 See docs for the format of the collation document here:
146 <https://docs.mongodb.com/master/reference/collation/>.
147
148 • "unique" — enforce uniqueness when true; inserting a duplicate
149 document (or creating one with update modifiers) will raise an
150 error.
151
152 • "name" — a name (string) for the index; one will be generated if
153 this is omitted.
154
155 Global options specified as the last value can contain the following
156 keys:
157
158 • "maxTimeMS" — maximum time in milliseconds before the operation
159 will time out.
160
161 drop_one
162 $output = $indexes->drop_one( $name );
163 $output = $indexes->drop_one( $name, \%options );
164
165 This method takes the name of an index and drops it. It returns the
166 output of the dropIndexes command (a hash reference) on success or
167 throws a exception if the command errors. However, if the index does
168 not exist, the command output will have the "ok" field as a false
169 value, but no exception will e thrown.
170
171 Valid options are:
172
173 • "maxTimeMS" — maximum time in milliseconds before the operation
174 will time out.
175
176 drop_all
177 $output = $indexes->drop_all;
178 $output = $indexes->drop_all(\%options);
179
180 This method drops all indexes (except the one on the "_id" field). It
181 returns the output of the dropIndexes command (a hash reference) on
182 success or throws a exception if the command fails.
183
184 Valid options are:
185
186 • "maxTimeMS" — maximum time in milliseconds before the operation
187 will time out.
188
190 • David Golden <david@mongodb.com>
191
192 • Rassi <rassi@mongodb.com>
193
194 • Mike Friedman <friedo@friedo.com>
195
196 • Kristina Chodorow <k.chodorow@gmail.com>
197
198 • Florian Ragwitz <rafl@debian.org>
199
201 This software is Copyright (c) 2020 by MongoDB, Inc.
202
203 This is free software, licensed under:
204
205 The Apache License, Version 2.0, January 2004
206
207
208
209perl v5.36.0 2023-01-20 MongoDB::IndexView(3)