Our MongoDB Perl Driver interface is compatible
with the official MongoDB Perl Driver on CPAN. Just add this line to your old project: use BuyLibs::MDB qw(-compat);
WHILE THE OFFICIAL MONGODB PERL DRIVER HAS BEEN DISCONTINUED, OUR SOLUTION OFFERS ASYNCHRONOUS MODEL, POOL OF CONNECTIONS, ADDITIONAL FEATURES, IMPROVED PERFORMANCE, AND COMPREHENSIVE SUPPORT.
MongoDB database client driver from BuyLibs™ is designed for use with Perl. The support for Perl provides developers with a powerful approach to managing database connections and operations.
We offer a reliable and feature-rich MongoDB Perl driver as a commercial alternative, providing ongoing support, updates, and compatibility with the latest MongoDB versions.
#!/usr/bin/env perl
# This is a Mojolicious application that demonstrates the use of BuyLibs
# asynchronous MongoDB operations for building a chat application.
# The application utilizes Mojo's built-in async/await functionality to interact
# with MongoDB in a non-blocking, high-performance manner.
#
# Benefits of BuyLibs asynchronous MongoDB usage with Mojo:
#
# 1. NON-BLOCKING I/O OPERATIONS: By using BuyLibs asynchronous MongoDB
# connections, the application avoids blocking the event loop while
# performing database operations. This enables the server to handle many
# requests concurrently, improving overall performance and scalability,
# especially in chat applications with frequent, small updates like message
# sending/receiving.
#
# 2. BUILT-IN SUPPORT FOR PROMISES: Mojo's async/await syntax makes it easy to
# manage asynchronous operations. The syntax is simple and readable, allowing
# developers to write non-blocking code that looks and behaves like
# synchronous code, reducing complexity.
#
# 3. PARALLEL EXECUTION: BuyLibs asynchronous MongoDB usage helps to parallelize
# I/O-bound tasks (e.g., fetching messages from the database) without waiting
# for one task to finish before starting the next, leading to faster response
# times.
#
use Mojolicious::Lite -signatures;
use Mojo::EventEmitter;
use Mojo::Base -async_await;
use BuyLibs::MDB qw(-compat); # The "-compat" option allows creating and using
# the new MongoDB client in the same way as the
# original MongoDB:: module.
$ARGV[0] //= 'daemon';
# Initialize the MongoDB connection and database, with specific connection
# pooling settings that enable parallel, non-blocking operations.
helper mongodb => sub {
state $cli = MongoDB->mojo_connect($ENV{MONGOD} || 'mongodb://localhost');
# Increase the default connection pool size from 100 to 150 if needed.
$cli->pool->max_size(150);
state $db = $cli->db('buylibs_mdb_examples_chatapp');
};
helper events => sub { state $events = Mojo::EventEmitter->new };
async sub load_messages ($c) {
# Load the latest chat messages asynchronously from MongoDB, in the same way
# as the MongoDB:: module, but using "await" for non-blocking IO.
my @messages =
await $c->mongodb->coll('messages')->find({})->sort({ '_id' => -1 })
->limit(20)->all;
return reverse @messages;
}
get '/' => async sub ($c) {
# Asynchronously fetch previous chat messages from MongoDB to display
# on the web page.
my @messages = await load_messages($c);
# Render the previous messages on the frontend.
$c->render(template => 'chat', messages => \@messages);
};
websocket '/channel' => sub ($c) {
$c->inactivity_timeout(3600);
# Handle incoming messages.
$c->on(
message => async sub ($c, $msg) {
# Save the new message asynchronously to MongoDB.
await $c->mongodb->coll('messages')
->insert_one({ message => $msg, timestamp => time });
# Broadcast the message to all connected clients.
$c->events->emit(mojochat => $msg);
}
);
# Send the message to the client.
my $cb = $c->events->on(mojochat => sub { $c->send(pop) });
# Unsubscribe when the connection is finished.
$c->on(
finish => sub ($c, $code, $reason = undef) {
$c->events->unsubscribe(mojochat => $cb);
}
);
};
app->start;
__DATA__
@@ chat.html.ep
<form onsubmit="sendMessage(this.children[0]); return false"><input></form>
<div id="history">
<% for my $message (@$messages) { %>
<p><%= $message->{message} %></p>
<% } %>
</div>
<script>
const ws = new WebSocket('<%= url_for('channel')->to_abs %>');
ws.onmessage = function (e) {
document.getElementById('history').innerHTML += '<p>' + e.data + '</p>';
};
function sendMessage(input) { ws.send(input.value); input.value = '' }
</script>
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use Try::Tiny;
use Safe::Isa;
use BuyLibs::MDB qw(-compat); # The "-compat" option allows creating and using
# the new MongoDB client in the same way as the
# original MongoDB:: module.
my $client = BuyLibs::MDB->connect($ENV{MONGOD} || 'mongodb://localhost');
my $db = $client->get_database('test_db_for_mdb');
my $collection = $db->get_collection('sample');
$collection->drop;
# Create a unique index on the 'name' field and insert multiple documents
$collection->indexes->create_one({ name => 1 }, { unique => 1 });
my @docs = (
{ name => 'Example1', type => 'Normal' },
{ name => 'Example2', type => 'Normal' },
{ name => 'Example3', type => 'Normal' },
);
for my $doc (@docs) {
$collection->insert_one($doc);
print "Inserted document with name: $doc->{name}\n";
}
# Insert a document with a duplicate name to trigger the error
try {
$collection->insert_one({ name => 'Example1', type => 'DuplicateTest' });
} catch {
if ($_->$_isa('MongoDB::DuplicateKeyError')) {
if ( $_->code == 11000
&& $_->message =~ /E11000 duplicate key error collection:/
&& $_->message =~ /test_db_for_mdb.sample index: name_1 dup key/
&& $_->result->$_isa('BuyLibs::MDB::InsertOneResult')
&& !defined $_->result->inserted_id
&& $_->result->write_errors->[0]->{code} == 11000
&& $_->result->write_errors->[0]->{keyPattern}{name} == 1
&& $_->result->write_errors->[0]->{keyValue}{name} eq 'Example1')
{
print "Duplicate key error (as expected): 'name' already exists.\n";
} else {
print "Unexpected duplicate key: ", Dumper($_), "\n";
}
} else {
die "Error inserting document: $@";
}
};
# Find documents and iterate using a cursor
my $cursor = $collection->find();
while (my $doc = $cursor->next) {
print "Found document: name = $doc->{name}, type = $doc->{type}\n";
}
$db->drop;
You can find more examples for our MongoDB Perl Driver on GitHub.
The table below outlines the versions of the MongoDB Perl driver to use with a particular MongoDB version.
The first column lists the driver version.
Perl Driver Version | MongoDB 8.0 | MongoDB 7.0 | MongoDB 6.0 | MongoDB 5.0 | MongoDB 4.4 | MongoDB 4.2 | MongoDB 4.0 | MongoDB 3.6 |
---|---|---|---|---|---|---|---|---|
8.0+ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
7.0 | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
6.0 | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
5.0 | ⊛ | ⊛ | ⊛ | ✓ | ✓ | ✓ | ✓ | ✓ |
Legend
Icon | Explanation |
---|---|
✓ | Full feature endorsement is suggested. |
⊛ | The driver version will work with the specified MongoDB version; however, it may not fully support all new features introduced in MongoDB. |
No mark | Compatibility testing between the driver version and the specified MongoDB version has not been performed. |
The module offers robust support for multiple programming paradigms:
All licenses include: |
|||
Perpetual License: Your license code ensures indefinite access to your purchased library version.
|
Free Updates: Enjoy one year of updates for the latest features and enhancements.
|
||
Unlimited Usage: Utilize the BuyLibs library across multiple computers or servers without limitations.
|
Royalty-Free Redistributables: Include library redistributables in deployment packages without additional royalties.
|
||
Flexible API Usage: Utilize the API across multiple applications and distribute freely to end-users.
|
Permanent License Codes: Your codes never expire, ensuring perpetual access to your library version.
|
Get in touch or purchase your license key today.
This website uses cookies to ensure you get the best experience on our website. You agree to our cookies if you continue to use our website.