.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Mango 3pm" .TH Mango 3pm "2018-04-14" "perl v5.26.1" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mango \- Pure\-Perl non\-blocking I/O MongoDB driver .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Mango; \& use feature state; \& \& # Declare a Mango helper \& sub mango { state $m = Mango\->new(\*(Aqmongodb://localhost:27017\*(Aq) } \& # or in a Mojolicious::Lite app \& helper mango => sub { state $m = Mango\->new(\*(Aqmongodb://localhost:27017\*(Aq) }; \& \& # Insert document \& my $oid = mango\->db(\*(Aqtest\*(Aq)\->collection(\*(Aqfoo\*(Aq)\->insert({bar => \*(Aqbaz\*(Aq}); \& \& # Find document \& my $doc = mango\->db(\*(Aqtest\*(Aq)\->collection(\*(Aqfoo\*(Aq)\->find_one({bar => \*(Aqbaz\*(Aq}); \& say $doc\->{bar}; \& \& # Update document \& mango\->db(\*(Aqtest\*(Aq)\->collection(\*(Aqfoo\*(Aq) \& \->update({bar => \*(Aqbaz\*(Aq}, {bar => \*(Aqyada\*(Aq}); \& \& # Remove document \& mango\->db(\*(Aqtest\*(Aq)\->collection(\*(Aqfoo\*(Aq)\->remove({bar => \*(Aqyada\*(Aq}); \& \& # Insert document with special BSON types \& use Mango::BSON \*(Aq:bson\*(Aq; \& my $oid = mango\->db(\*(Aqtest\*(Aq)\->collection(\*(Aqfoo\*(Aq) \& \->insert({data => bson_bin("\ex00\ex01"), now => bson_time}); \& \& # Non\-blocking concurrent find \& my $delay = Mojo::IOLoop\->delay(sub { \& my ($delay, @docs) = @_; \& ... \& }); \& for my $name (qw(sri marty)) { \& my $end = $delay\->begin(0); \& mango\->db(\*(Aqtest\*(Aq)\->collection(\*(Aqusers\*(Aq)\->find({name => $name})\->all(sub { \& my ($cursor, $err, $docs) = @_; \& $end\->(@$docs); \& }); \& } \& $delay\->wait; \& \& # Event loops such as AnyEvent are supported through EV \& use EV; \& use AnyEvent; \& my $cv = AE::cv; \& mango\->db(\*(Aqtest\*(Aq)\->command(buildInfo => sub { \& my ($db, $err, $doc) = @_; \& $cv\->send($doc\->{version}); \& }); \& say $cv\->recv; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mango is a pure-Perl non-blocking I/O MongoDB driver, optimized for use with the Mojolicious real-time web framework, and with multiple event loop support. Since MongoDB is still changing rapidly, only the latest stable version is supported. .PP For MongoDB 2.6 support, use Mango 1.16. .PP To learn more about MongoDB you should take a look at the official documentation , the documentation included in this distribution is no replacement for it. .PP Look at Mango::Collection for \s-1CRUD\s0 operations. .PP Many arguments passed to methods as well as values of attributes get serialized to \s-1BSON\s0 with Mango::BSON, which provides many helper functions you can use to generate data types that are not available natively in Perl. All connections will be reset automatically if a new process has been forked, this allows multiple processes to share the same Mango object safely. .PP For better scalability (epoll, kqueue) and to provide IPv6, \s-1SOCKS5\s0 as well as \&\s-1TLS\s0 support, the optional modules \s-1EV\s0 (4.0+), IO::Socket::IP (0.20+), IO::Socket::Socks (0.64+) and IO::Socket::SSL (1.84+) will be used automatically if they are installed. Individual features can also be disabled with the \f(CW\*(C`MOJO_NO_IPV6\*(C'\fR, \f(CW\*(C`MOJO_NO_SOCKS\*(C'\fR and \f(CW\*(C`MOJO_NO_TLS\*(C'\fR environment variables. .SH "EVENTS" .IX Header "EVENTS" Mango inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS "connection" .IX Subsection "connection" .Vb 4 \& $mango\->on(connection => sub { \& my ($mango, $id) = @_; \& ... \& }); .Ve .PP Emitted when a new connection has been established. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mango implements the following attributes. .SS "default_db" .IX Subsection "default_db" .Vb 2 \& my $name = $mango\->default_db; \& $mango = $mango\->default_db(\*(Aqtest\*(Aq); .Ve .PP Default database, defaults to \f(CW\*(C`admin\*(C'\fR. .SS "hosts" .IX Subsection "hosts" .Vb 2 \& my $hosts = $mango\->hosts; \& $mango = $mango\->hosts([[\*(Aqlocalhost\*(Aq, 3000], [\*(Aqlocalhost\*(Aq, 4000]]); .Ve .PP Servers to connect to, defaults to \f(CW\*(C`localhost\*(C'\fR and port \f(CW27017\fR. .SS "inactivity_timeout" .IX Subsection "inactivity_timeout" .Vb 2 \& my $timeout = $mango\->inactivity_timeout; \& $mango = $mango\->inactivity_timeout(15); .Ve .PP Maximum amount of time in seconds a connection can be inactive before getting closed, defaults to \f(CW0\fR. Setting the value to \f(CW0\fR will allow connections to be inactive indefinitely. .SS "ioloop" .IX Subsection "ioloop" .Vb 2 \& my $loop = $mango\->ioloop; \& $mango = $mango\->ioloop(Mojo::IOLoop\->new); .Ve .PP Event loop object to use for blocking I/O operations, defaults to a Mojo::IOLoop object. .SS "j" .IX Subsection "j" .Vb 2 \& my $j = $mango\->j; \& $mango = $mango\->j(1); .Ve .PP Wait for all operations to have reached the journal, defaults to \f(CW0\fR. .SS "max_bson_size" .IX Subsection "max_bson_size" .Vb 2 \& my $max = $mango\->max_bson_size; \& $mango = $mango\->max_bson_size(16777216); .Ve .PP Maximum size for \s-1BSON\s0 documents in bytes, defaults to \f(CW16777216\fR (16MB). .SS "max_connections" .IX Subsection "max_connections" .Vb 2 \& my $max = $mango\->max_connections; \& $mango = $mango\->max_connections(5); .Ve .PP Maximum number of connections to use for non-blocking operations, defaults to \&\f(CW5\fR. .SS "max_write_batch_size" .IX Subsection "max_write_batch_size" .Vb 2 \& my $max = $mango\->max_write_batch_size; \& $mango = $mango\->max_write_batch_size(1000); .Ve .PP Maximum number of write operations to batch together, defaults to \f(CW1000\fR. .SS "protocol" .IX Subsection "protocol" .Vb 2 \& my $protocol = $mango\->protocol; \& $mango = $mango\->protocol(Mango::Protocol\->new); .Ve .PP Protocol handler, defaults to a Mango::Protocol object. .SS "w" .IX Subsection "w" .Vb 2 \& my $w = $mango\->w; \& $mango = $mango\->w(2); .Ve .PP Wait for all operations to have reached at least this many servers, \f(CW1\fR indicates just primary, \f(CW2\fR indicates primary and at least one secondary, defaults to \f(CW1\fR. .SS "wtimeout" .IX Subsection "wtimeout" .Vb 2 \& my $timeout = $mango\->wtimeout; \& $mango = $mango\->wtimeout(1); .Ve .PP Timeout for write propagation in milliseconds, defaults to \f(CW1000\fR. .SH "METHODS" .IX Header "METHODS" Mango inherits all methods from Mojo::Base and implements the following new ones. .SS "backlog" .IX Subsection "backlog" .Vb 1 \& my $num = $mango\->backlog; .Ve .PP Number of queued operations that have not yet been assigned to a connection. .SS "db" .IX Subsection "db" .Vb 2 \& my $db = $mango\->db; \& my $db = $mango\->db(\*(Aqtest\*(Aq); .Ve .PP Build Mango::Database object for database, uses \*(L"default_db\*(R" if no name is provided. Note that the reference \*(L"mango\*(R" in Mango::Database is weakened, so the Mango object needs to be referenced elsewhere as well. .SS "from_string" .IX Subsection "from_string" .Vb 2 \& $mango \& = $mango\->from_string(\*(Aqmongodb://sri:s3cret@localhost:3000/test?w=2\*(Aq); .Ve .PP Parse configuration from connection string. .SS "get_more" .IX Subsection "get_more" .Vb 1 \& my $reply = $mango\->get_more($namespace, $return, $cursor); .Ve .PP Perform low level \f(CW\*(C`GET_MORE\*(C'\fR operation. You can also append a callback to perform operation non-blocking. .PP .Vb 5 \& $mango\->get_more(($namespace, $return, $cursor) => sub { \& my ($mango, $err, $reply) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS "kill_cursors" .IX Subsection "kill_cursors" .Vb 1 \& $mango\->kill_cursors(@ids); .Ve .PP Perform low level \f(CW\*(C`KILL_CURSORS\*(C'\fR operation. You can also append a callback to perform operation non-blocking. .PP .Vb 5 \& $mango\->kill_cursors(@ids => sub { \& my ($mango, $err) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SS "new" .IX Subsection "new" .Vb 2 \& my $mango = Mango\->new; \& my $mango = Mango\->new(\*(Aqmongodb://sri:s3cret@localhost:3000/test?w=2\*(Aq); \& \& # Using TLS encryption \& my $mango = Mango\->new(\*(Aqmongodb://127.0.0.1:27017\*(Aq, tls => 1, \& tls_cert => \*(Aq/path/to/certificate.pem\*(Aq); .Ve .PP Construct a new Mango object and parse connection string with \&\*(L"from_string\*(R" if necessary. .PP Not that is is \fBstrongly\fR recommended to build your Mango object inside a helper function like shown in the synopsis. This is because the Mango's object reference inside Mango::Database objects is weakened to avoid memory leaks. This means your Mango instance is quickly going to get undefined after you use the \f(CW\*(C`db\*(C'\fR method. So, use a helper to prevent that. .PP If a username and password are provided, Mango will try to authenticate using \&\s-1SCRAM\-SHA1.\s0 \fBWarning:\fR this will require Authen::SCRAM which is not installed by default. .PP Any extra arguments given after the connection string will be passed to the \&\f(CW\*(C`connect\*(C'\fR method from Mojo::IOLoop::Client. To connect to a server using \&\s-1TLS,\s0 use the options \f(CW\*(C`tls\*(C'\fR (boolean), \f(CW\*(C`tls_cert\*(C'\fR and optionally \f(CW\*(C`tls_ca\*(C'\fR. .SS "query" .IX Subsection "query" .Vb 2 \& my $reply \& = $mango\->query($namespace, $flags, $skip, $return, $query, $fields); .Ve .PP Perform low level \f(CW\*(C`QUERY\*(C'\fR operation. You can also append a callback to perform operation non-blocking. .PP .Vb 5 \& $mango\->query(($namespace, $flags, $skip, $return, $query, $fields) => sub { \& my ($mango, $err, $reply) = @_; \& ... \& }); \& Mojo::IOLoop\->start unless Mojo::IOLoop\->is_running; .Ve .SH "DEBUGGING" .IX Header "DEBUGGING" You can set the \f(CW\*(C`MANGO_DEBUG\*(C'\fR environment variable to get some advanced diagnostics information printed to \f(CW\*(C`STDERR\*(C'\fR. .PP .Vb 1 \& MANGO_DEBUG=1 .Ve .SH "SPONSORS" .IX Header "SPONSORS" Some of the work on this distribution has been sponsored by Drip Depot , thank you! .SH "AUTHOR" .IX Header "AUTHOR" Sebastian Riedel, \f(CW\*(C`sri@cpan.org\*(C'\fR. .PP Current maintainer: Olivier Duclos \f(CW\*(C`odc@cpan.org\*(C'\fR. .SH "CREDITS" .IX Header "CREDITS" In alphabetical order: .Sp .RS 2 alexbyk .Sp Andrey Khozov .Sp Colin Cyr .RE .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright (C) 2013\-2014, Sebastian Riedel. .PP This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. .SH "SEE ALSO" .IX Header "SEE ALSO" , Mojolicious::Guides, .