/usr/share/perl5/Mango/Collection.pm is in libmango-perl 1.29-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 | package Mango::Collection;
use Mojo::Base -base;
use Carp 'croak';
use Mango::BSON qw(bson_code bson_doc bson_oid);
use Mango::Bulk;
use Mango::Cursor;
use Mango::Cursor::Query;
has [qw(db name)];
sub aggregate {
my ($self, $pipeline) = (shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my $command = bson_doc(aggregate => $self->name, pipeline => $pipeline,
%{shift // {}});
$command->{cursor} //= {} unless $command->{explain};
# Blocking
return $self->_aggregate($command, $self->db->command($command)) unless $cb;
# Non-blocking
return $self->db->command($command,
sub { shift; $self->$cb(shift, $self->_aggregate($command, shift)) });
}
sub build_index_name { join '_', keys %{$_[1]} }
sub bulk { Mango::Bulk->new(collection => shift) }
sub create {
my $self = shift;
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
return $self->_command(bson_doc(create => $self->name, %{shift // {}}), $cb);
}
sub drop { $_[0]->_command(bson_doc(drop => $_[0]->name), $_[1]) }
sub drop_index {
my ($self, $name) = (shift, shift);
return $self->_command(bson_doc(dropIndexes => $self->name, index => $name),
shift);
}
sub ensure_index {
my ($self, $spec) = (shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my $doc = shift // {};
$doc->{name} //= $self->build_index_name($spec);
$doc->{key} = $spec;
# Non-blocking
my $command = bson_doc createIndexes => $self->name, indexes => [$doc];
return $self->db->command($command => sub { shift; $self->$cb(shift) })
if $cb;
# Blocking
$self->db->command($command);
}
sub find {
Mango::Cursor::Query->new(
collection => shift,
query => shift // {},
fields => shift // {}
);
}
sub find_and_modify {
my ($self, $opts) = (shift, shift);
return $self->_command(bson_doc(findAndModify => $self->name, %$opts),
shift, sub { shift->{value} });
}
sub find_one {
my ($self, $query) = (shift, shift);
$query = {_id => $query} if ref $query eq 'Mango::BSON::ObjectID';
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
# Non-blocking
my $cursor = $self->find($query, @_)->limit(-1);
return $cursor->next(sub { shift; $self->$cb(@_) }) if $cb;
# Blocking
return $cursor->next;
}
sub full_name { join '.', $_[0]->db->name, $_[0]->name }
sub index_information {
my $self = shift;
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my $cmd = bson_doc(listIndexes => $self->name, @_);
$self->_command($cmd, $cb,
sub {
my $doc = shift or return bson_doc;
bson_doc map { delete $_->{ns}; (delete $_->{name}, $_) }
@{$doc->{cursor}->{firstBatch}};
}
);
}
sub insert {
my ($self, $orig_docs, $cb) = @_;
$orig_docs = [$orig_docs] unless ref $orig_docs eq 'ARRAY';
# Make a shallow copy of the documents and add an id if needed
my @docs = map { bson_doc %$_ } @$orig_docs;
my @ids = map { $_->{_id} //= bson_oid } @docs;
my $command = bson_doc
insert => $self->name,
documents => \@docs,
ordered => \1,
writeConcern => $self->db->build_write_concern;
return $self->_command($command, $cb, sub { @ids > 1 ? \@ids : $ids[0] });
}
sub map_reduce {
my ($self, $map, $reduce) = (shift, shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my $command = bson_doc
mapreduce => $self->name,
map => ref $map ? $map : bson_code($map),
reduce => ref $reduce ? $reduce : bson_code($reduce),
%{shift // {}};
# Blocking
return $self->_map_reduce($self->db->command($command)) unless $cb;
# Non-blocking
return $self->db->command(
$command => sub { shift; $self->$cb(shift, $self->_map_reduce(shift)) });
}
sub options {
my ($self, $cb) = @_;
my $cmd = bson_doc(listCollections => 1, filter => { name => $self->name });
$self->_command($cmd, $cb, sub { shift->{cursor}->{firstBatch}->[0] });
}
sub remove {
my $self = shift;
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my $query = shift // {};
my $flags = shift // {};
($query, $flags) = ({_id => $query}, {single => 1})
if ref $query eq 'Mango::BSON::ObjectID';
my $command = bson_doc
delete => $self->name,
deletes => [{q => $query, limit => $flags->{single} ? 1 : 0}],
ordered => \1,
writeConcern => $self->db->build_write_concern;
return $self->_command($command, $cb);
}
sub rename {
my ($self, $name, $cb) = @_;
my $admin = $self->db->mango->db('admin');
my $dbname = $self->db->name;
my $oldname = join '.', $dbname, $self->name;
my $newname = join '.', $dbname, $name;
my $cmd = bson_doc renameCollection => $oldname, to => $newname;
# Non-blocking
return $admin->command($cmd, sub {
my ($admin_db, $err, $doc) = @_;
my $newcol = $doc->{ok} ? $self->db->collection($name) : undef;
return $cb->($self, $err, $newcol);
}) if $cb;
# Blocking
my $doc = $admin->command($cmd);
return $doc->{ok} ? $self->db->collection($name) : undef;
}
sub save {
my ($self, $doc, $cb) = @_;
# New document
return $self->insert($doc, $cb) unless $doc->{_id};
# Update non-blocking
my @update = ({_id => $doc->{_id}}, $doc, {upsert => 1});
return $self->update(@update => sub { shift->$cb(shift, $doc->{_id}) })
if $cb;
# Update blocking
$self->update(@update);
return $doc->{_id};
}
sub stats { $_[0]->_command(bson_doc(collstats => $_[0]->name), $_[1]) }
sub update {
my ($self, $query, $update) = (shift, shift, shift);
my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
my $flags = shift // {};
$update = {
q => ref $query eq 'Mango::BSON::ObjectID' ? {_id => $query} : $query,
u => $update,
upsert => $flags->{upsert} ? \1 : \0,
multi => $flags->{multi} ? \1 : \0
};
my $command = bson_doc
update => $self->name,
updates => [$update],
ordered => \1,
writeConcern => $self->db->build_write_concern;
return $self->_command($command, $cb);
}
sub _aggregate {
my ($self, $command, $doc) = @_;
# Document (explain)
return $doc if $command->{explain};
# Collection
my $out = $command->{pipeline}[-1]{'$out'};
return $self->db->collection($out) if defined $out;
# Cursor
my $cursor = $doc->{cursor};
return Mango::Cursor->new(collection => $self, id => $cursor->{id})
->add_batch($cursor->{firstBatch});
}
sub _command {
my ($self, $command, $cb, $return) = @_;
$return ||= sub {shift};
# Non-blocking
my $db = $self->db;
my $protocol = $db->mango->protocol;
return $db->command(
$command => sub {
my ($db, $err, $doc) = @_;
$err ||= $protocol->write_error($doc);
$self->$cb($err, $return->($doc));
}
) if $cb;
# Blocking
my $doc = $db->command($command);
if (my $err = $protocol->write_error($doc)) { croak $err }
return $return->($doc);
}
sub _map_reduce {
my ($self, $doc) = @_;
return $doc->{results} unless $doc->{result};
return $self->db->collection($doc->{result});
}
1;
=encoding utf8
=head1 NAME
Mango::Collection - MongoDB collection
=head1 SYNOPSIS
use Mango::Collection;
my $collection = Mango::Collection->new(db => $db);
my $cursor = $collection->find({foo => 'bar'});
=head1 DESCRIPTION
L<Mango::Collection> is a container for MongoDB collections used by
L<Mango::Database>.
=head1 ATTRIBUTES
L<Mango::Collection> implements the following attributes.
=head2 db
my $db = $collection->db;
$collection = $collection->db(Mango::Database->new);
L<Mango::Database> object this collection belongs to.
=head2 name
my $name = $collection->name;
$collection = $collection->name('bar');
Name of this collection.
=head1 METHODS
L<Mango::Collection> inherits all methods from L<Mojo::Base> and implements
the following new ones.
=head2 aggregate
my $cursor = $collection->aggregate(
[{'$group' => {_id => undef, total => {'$sum' => '$foo'}}}]);
my $collection = $collection->aggregate(
[{'$match' => {'$gt' => 23}}, {'$out' => 'some_collection'}]);
my $doc = $collection->aggregate(
[{'$match' => {'$gt' => 23}}], {explain => bson_true});
Aggregate collection with aggregation framework, additional options will be
passed along to the server verbatim. You can also append a callback to perform
operation non-blocking.
my $pipeline = [{'$group' => {_id => undef, total => {'$sum' => '$foo'}}}];
$collection->aggregate($pipeline => sub {
my ($collection, $err, $cursor) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 build_index_name
my $name = $collection->build_index_name(bson_doc(foo => 1, bar => -1));
my $name = $collection->build_index_name({foo => 1});
Build name for index specification, the order of keys matters for compound
indexes.
=head2 bulk
my $bulk = $collection->bulk;
Build L<Mango::Bulk> object.
my $bulk = $collection->bulk;
$bulk->insert({foo => $_}) for 1 .. 10;
$bulk->find({foo => 4})->update_one({'$set' => {bar => 'baz'}});
$bulk->find({foo => 7})->remove_one;
my $results = $bulk->execute;
=head2 create
$collection->create;
$collection->create({capped => bson_true, max => 5, size => 10000});
Create collection. You can also append a callback to perform operation
non-blocking.
$collection->create({capped => bson_true, max => 5, size => 10000} => sub {
my ($collection, $err) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 drop
$collection->drop;
Drop collection. You can also append a callback to perform operation
non-blocking.
$collection->drop(sub {
my ($collection, $err) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 drop_index
$collection->drop_index('foo');
Drop index. You can also append a callback to perform operation non-blocking.
$collection->drop_index(foo => sub {
my ($collection, $err) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 ensure_index
$collection->ensure_index(bson_doc(foo => 1, bar => -1));
$collection->ensure_index({foo => 1});
$collection->ensure_index({foo => 1}, {unique => bson_true});
Make sure an index exists, the order of keys matters for compound indexes,
additional options will be passed along to the server verbatim. You can also
append a callback to perform operation non-blocking.
$collection->ensure_index(({foo => 1}, {unique => bson_true}) => sub {
my ($collection, $err) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 find
my $cursor = $collection->find;
my $cursor = $collection->find({foo => 'bar'});
my $cursor = $collection->find({foo => 'bar'}, {foo => 1});
Build L<Mango::Cursor::Query> object for query.
# Exclude "_id" field from results
my $docs = $collection->find({foo => 'bar'}, {_id => 0})->all;
=head2 find_and_modify
my $doc = $collection->find_and_modify(
{query => {foo => 'bar'}, update => {'$set' => {foo => 'baz'}}});
Fetch and update or remove a document atomically. You can also append a callback
to perform operation non-blocking.
my $opts = {query => {foo => 'bar'}, update => {'$set' => {foo => 'baz'}}};
$collection->find_and_modify($opts => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
By default this method returns the unmodified version of the document. To
change this behaviour, add the option C<new =E<gt> 1>.
=head2 find_one
my $doc = $collection->find_one({foo => 'bar'});
my $doc = $collection->find_one({foo => 'bar'}, {foo => 1});
my $doc = $collection->find_one($oid, {foo => 1});
Find one document. You can also append a callback to perform operation
non-blocking.
$collection->find_one({foo => 'bar'} => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 full_name
my $name = $collection->full_name;
Full name of this collection.
=head2 index_information
my $info = $collection->index_information;
# return only the 5 first indexes
my $info = $collection->index_information(cursor => { batchSize => 5 });
Get index information for collection. You can also append a callback to
perform operation non-blocking.
$collection->index_information(sub {
my ($collection, $err, $info) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 insert
my $oid = $collection->insert({foo => 'bar'});
my $oids = $collection->insert([{foo => 'bar'}, {baz => 'yada'}]);
Insert one or more documents into collection. You can also append a callback
to perform operation non-blocking.
$collection->insert({foo => 'bar'} => sub {
my ($collection, $err, $oid) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Note that C<insert> has to ensure each document has an C<_id> before sending
them to MongoDB. To avoid modifying your data, it makes a copy of the
documents. This can be a bit slow if you are sending big objects like
pictures. To avoid that, consider using C<save> instead.
=head2 map_reduce
my $collection = $collection->map_reduce($map, $reduce, {out => 'foo'});
my $docs = $collection->map_reduce($map, $reduce, {out => {inline => 1}});
my $docs = $collection->map_reduce(
bson_code($map), bson_code($reduce), {out => {inline => 1}});
Perform map/reduce operation on collection, additional options will be passed
along to the server verbatim. You can also append a callback to perform
operation non-blocking.
$collection->map_reduce(($map, $reduce, {out => {inline => 1}}) => sub {
my ($collection, $err, $docs) = @_;
...
}
);
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 options
my $doc = $collection->options;
Get options for collection. You can also append a callback to perform
operation non-blocking.
$collection->options(sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 remove
my $result = $collection->remove;
my $result = $collection->remove($oid);
my $result = $collection->remove({foo => 'bar'});
my $result = $collection->remove({foo => 'bar'}, {single => 1});
Remove documents from collection. You can also append a callback to perform
operation non-blocking. Returns a WriteResult document.
$collection->remove(({foo => 'bar'}, {single => 1}) => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
These options are currently available:
=over 2
=item single
single => 1
Remove only one document.
=back
=head2 rename
my $new_collection = $collection->rename('NewName');
Rename a collection, keeping all of its original contents and options. Returns
a new Mango::Collection object pointing to the renamed collection. You can
also append a callback to perform operation non-blocking.
$collection->rename('NewName' => sub {
my ($collection, $err, $oid) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 save
my $oid = $collection->save({foo => 'bar'});
Save document to collection. The document MUST have an C<_id>. You can also
append a callback to perform operation non-blocking.
$collection->save({foo => 'bar'} => sub {
my ($collection, $err, $oid) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 stats
my $stats = $collection->stats;
Get collection statistics. You can also append a callback to perform operation
non-blocking.
$collection->stats(sub {
my ($collection, $err, $stats) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
=head2 update
my $result = $collection->update($oid, {foo => 'baz'});
my $result = $collection->update({foo => 'bar'}, {foo => 'baz'});
my $result = $collection->update({foo => 'bar'}, {foo => 'baz'}, {multi => 1});
Update document in collection. You can also append a callback to perform
operation non-blocking. Returns a WriteResult document.
$collection->update(({foo => 'bar'}, {foo => 'baz'}, {multi => 1}) => sub {
my ($collection, $err, $doc) = @_;
...
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
These options are currently available:
=over 2
=item multi
multi => 1
Update more than one document.
=item upsert
upsert => 1
Insert document if none could be updated.
=back
=head1 SEE ALSO
L<Mango>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|