1Mango::Collection(3)  User Contributed Perl Documentation Mango::Collection(3)
2
3
4

NAME

6       Mango::Collection - MongoDB collection
7

SYNOPSIS

9         use Mango::Collection;
10
11         my $collection = Mango::Collection->new(db => $db);
12         my $cursor     = $collection->find({foo => 'bar'});
13

DESCRIPTION

15       Mango::Collection is a container for MongoDB collections used by
16       Mango::Database.
17

ATTRIBUTES

19       Mango::Collection implements the following attributes.
20
21   db
22         my $db      = $collection->db;
23         $collection = $collection->db(Mango::Database->new);
24
25       Mango::Database object this collection belongs to.
26
27   name
28         my $name    = $collection->name;
29         $collection = $collection->name('bar');
30
31       Name of this collection.
32

METHODS

34       Mango::Collection inherits all methods from Mojo::Base and implements
35       the following new ones.
36
37   aggregate
38         my $cursor = $collection->aggregate(
39           [{'$group' => {_id => undef, total => {'$sum' => '$foo'}}}]);
40         my $collection = $collection->aggregate(
41           [{'$match' => {'$gt' => 23}}, {'$out' => 'some_collection'}]);
42         my $doc = $collection->aggregate(
43           [{'$match' => {'$gt' => 23}}], {explain => bson_true});
44
45       Aggregate collection with aggregation framework, additional options
46       will be passed along to the server verbatim. You can also append a
47       callback to perform operation non-blocking.
48
49         my $pipeline = [{'$group' => {_id => undef, total => {'$sum' => '$foo'}}}];
50         $collection->aggregate($pipeline => sub {
51           my ($collection, $err, $cursor) = @_;
52           ...
53         });
54         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
55
56   build_index_name
57         my $name = $collection->build_index_name(bson_doc(foo => 1, bar => -1));
58         my $name = $collection->build_index_name({foo => 1});
59
60       Build name for index specification, the order of keys matters for
61       compound indexes.
62
63   bulk
64         my $bulk = $collection->bulk;
65
66       Build Mango::Bulk object.
67
68         my $bulk = $collection->bulk;
69         $bulk->insert({foo => $_}) for 1 .. 10;
70         $bulk->find({foo => 4})->update_one({'$set' => {bar => 'baz'}});
71         $bulk->find({foo => 7})->remove_one;
72         my $results = $bulk->execute;
73
74   create
75         $collection->create;
76         $collection->create({capped => bson_true, max => 5, size => 10000});
77
78       Create collection. You can also append a callback to perform operation
79       non-blocking.
80
81         $collection->create({capped => bson_true, max => 5, size => 10000} => sub {
82           my ($collection, $err) = @_;
83           ...
84         });
85         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
86
87   drop
88         $collection->drop;
89
90       Drop collection. You can also append a callback to perform operation
91       non-blocking.
92
93         $collection->drop(sub {
94           my ($collection, $err) = @_;
95           ...
96         });
97         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
98
99   drop_index
100         $collection->drop_index('foo');
101
102       Drop index. You can also append a callback to perform operation non-
103       blocking.
104
105         $collection->drop_index(foo => sub {
106           my ($collection, $err) = @_;
107           ...
108         });
109         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
110
111   ensure_index
112         $collection->ensure_index(bson_doc(foo => 1, bar => -1));
113         $collection->ensure_index({foo => 1});
114         $collection->ensure_index({foo => 1}, {unique => bson_true});
115
116       Make sure an index exists, the order of keys matters for compound
117       indexes, additional options will be passed along to the server
118       verbatim. You can also append a callback to perform operation non-
119       blocking.
120
121         $collection->ensure_index(({foo => 1}, {unique => bson_true}) => sub {
122           my ($collection, $err) = @_;
123           ...
124         });
125         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
126
127   find
128         my $cursor = $collection->find;
129         my $cursor = $collection->find({foo => 'bar'});
130         my $cursor = $collection->find({foo => 'bar'}, {foo => 1});
131
132       Build Mango::Cursor::Query object for query.
133
134         # Exclude "_id" field from results
135         my $docs = $collection->find({foo => 'bar'}, {_id => 0})->all;
136
137   find_and_modify
138         my $doc = $collection->find_and_modify(
139           {query => {foo => 'bar'}, update => {'$set' => {foo => 'baz'}}});
140
141       Fetch and update or remove a document atomically. You can also append a
142       callback to perform operation non-blocking.
143
144         my $opts = {query => {foo => 'bar'}, update => {'$set' => {foo => 'baz'}}};
145         $collection->find_and_modify($opts => sub {
146           my ($collection, $err, $doc) = @_;
147           ...
148         });
149         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
150
151       By default this method returns the unmodified version of the document.
152       To change this behaviour, add the option "new => 1".
153
154   find_one
155         my $doc = $collection->find_one({foo => 'bar'});
156         my $doc = $collection->find_one({foo => 'bar'}, {foo => 1});
157         my $doc = $collection->find_one($oid, {foo => 1});
158
159       Find one document. You can also append a callback to perform operation
160       non-blocking.
161
162         $collection->find_one({foo => 'bar'} => sub {
163           my ($collection, $err, $doc) = @_;
164           ...
165         });
166         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
167
168   full_name
169         my $name = $collection->full_name;
170
171       Full name of this collection.
172
173   index_information
174         my $info = $collection->index_information;
175         # return only the 5 first indexes
176         my $info = $collection->index_information(cursor => { batchSize => 5 });
177
178       Get index information for collection. You can also append a callback to
179       perform operation non-blocking.
180
181         $collection->index_information(sub {
182           my ($collection, $err, $info) = @_;
183           ...
184         });
185         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
186
187   insert
188         my $oid  = $collection->insert({foo => 'bar'});
189         my $oids = $collection->insert([{foo => 'bar'}, {baz => 'yada'}]);
190
191       Insert one or more documents into collection. You can also append a
192       callback to perform operation non-blocking.
193
194         $collection->insert({foo => 'bar'} => sub {
195           my ($collection, $err, $oid) = @_;
196           ...
197         });
198         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
199
200       Note that "insert" has to ensure each document has an "_id" before
201       sending them to MongoDB. To avoid modifying your data, it makes a copy
202       of the documents. This can be a bit slow if you are sending big objects
203       like pictures. To avoid that, consider using "save" instead.
204
205   map_reduce
206         my $collection = $collection->map_reduce($map, $reduce, {out => 'foo'});
207         my $docs = $collection->map_reduce($map, $reduce, {out => {inline => 1}});
208         my $docs = $collection->map_reduce(
209           bson_code($map), bson_code($reduce), {out => {inline => 1}});
210
211       Perform map/reduce operation on collection, additional options will be
212       passed along to the server verbatim. You can also append a callback to
213       perform operation non-blocking.
214
215         $collection->map_reduce(($map, $reduce, {out => {inline => 1}}) => sub {
216             my ($collection, $err, $docs) = @_;
217             ...
218           }
219         );
220         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
221
222   options
223         my $doc = $collection->options;
224
225       Get options for collection. You can also append a callback to perform
226       operation non-blocking.
227
228         $collection->options(sub {
229           my ($collection, $err, $doc) = @_;
230           ...
231         });
232         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
233
234   remove
235         my $result = $collection->remove;
236         my $result = $collection->remove($oid);
237         my $result = $collection->remove({foo => 'bar'});
238         my $result = $collection->remove({foo => 'bar'}, {single => 1});
239
240       Remove documents from collection. You can also append a callback to
241       perform operation non-blocking. Returns a WriteResult document.
242
243         $collection->remove(({foo => 'bar'}, {single => 1}) => sub {
244           my ($collection, $err, $doc) = @_;
245           ...
246         });
247         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
248
249       These options are currently available:
250
251       single
252           single => 1
253
254         Remove only one document.
255
256   rename
257         my $new_collection = $collection->rename('NewName');
258
259       Rename a collection, keeping all of its original contents and options.
260       Returns a new Mango::Collection object pointing to the renamed
261       collection. You can also append a callback to perform operation non-
262       blocking.
263
264         $collection->rename('NewName' => sub {
265           my ($collection, $err, $oid) = @_;
266           ...
267         });
268         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
269
270   save
271         my $oid = $collection->save({foo => 'bar'});
272
273       Save document to collection. The document MUST have an "_id". You can
274       also append a callback to perform operation non-blocking.
275
276         $collection->save({foo => 'bar'} => sub {
277           my ($collection, $err, $oid) = @_;
278           ...
279         });
280         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
281
282   stats
283         my $stats = $collection->stats;
284
285       Get collection statistics. You can also append a callback to perform
286       operation non-blocking.
287
288         $collection->stats(sub {
289           my ($collection, $err, $stats) = @_;
290           ...
291         });
292         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
293
294   update
295         my $result = $collection->update($oid, {foo => 'baz'});
296         my $result = $collection->update({foo => 'bar'}, {foo => 'baz'});
297         my $result = $collection->update({foo => 'bar'}, {foo => 'baz'}, {multi => 1});
298
299       Update document in collection. You can also append a callback to
300       perform operation non-blocking. Returns a WriteResult document.
301
302         $collection->update(({foo => 'bar'}, {foo => 'baz'}, {multi => 1}) => sub {
303           my ($collection, $err, $doc) = @_;
304           ...
305         });
306         Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
307
308       These options are currently available:
309
310       multi
311           multi => 1
312
313         Update more than one document.
314
315       upsert
316           upsert => 1
317
318         Insert document if none could be updated.
319

SEE ALSO

321       Mango, Mojolicious::Guides, <http://mojolicio.us>.
322
323
324
325perl v5.34.0                      2022-01-21              Mango::Collection(3)
Impressum