MongoX - DSL sugar for MongoDB
version 0.05
# quick bootstrap, add connection and switch to db:'test' use MongoX ( host => 'mongodb://127.0.0.1',db => 'test' ); # common way use MongoX; #register default connection; add_connection host => '127.0.0.1'; # switch to default connection; use_connection; # use database 'test' use_db 'test'; #add/register another connection with id "remote2" add_connection host => '192.168.1.1',id => 'remote2'; # switch to this connection use_connection 'remote2'; #get a collection object (from the db in current context) my $foo = get_collection 'foo'; # use 'foo' as default context collection use_collection 'foo'; # use context's db/collection say 'total rows:',context_collection->count(); my $id = context_collection->insert({ name => 'Pan', home => 'Beijing' }); my $gridfs = context_db->get_gridfs; # loop dbs/collections for_dbs{ for_collections { db_ensure_index {created_on => 1}; } context_db->collection_names; } 'db1','db2';
MongoX is a light wrapper to MongoDB driver, it provide a very simple but handy DSL syntax. It also will provide some usefull helpers like builtin mongoshell, you can quick work with MongoDB.
MongoX takes a set of options for the class construction at compile time as a HASH parameter to the "use" line.
As a convenience, you can pass the default connection parameters and default database, then when MongoX import, it will apply these options to "add_connection" and "use_db", so the following code:
use MongoX ( host => 'mongodb://127.0.0.1',db => 'test' );
is equivalent to:
use MongoX; add_connection host => 'mongodb://127.0.0.1'; use_connection; use_db 'test';
context_connection,context_db, context_collection are implicit MongoDB::Connection, MongoDB::Database and MongoDB::Collection.
context_connection
context_db
context_collection
use_* keywords can make/switch implicit MongoDB object in context.
with_context allow build a sanbox to execute code block and do something, the context be restored when out the block.
These are loop keywords, it will switch related context object in the given list and loop run the code block.
my $db = context_db;
Return current MongoDB::Database object in context;
my $con = context_connection;
Return current MongoDB::Connection object in context.
my $col = context_collection;
Return current MongoDB::Collection object in context, you can replace the object with "use_collection".
# create a default connection use_connection; # use another connection with id:'con2' use_connection 'con2';
Switch to given connection, set the context connection to this connection.
use_db 'foo';
Switch to the database, set the context database to this database;
use_collection 'user'
Set 'user' collection as context collection.
add_connection id => 'default', host => 'mongodb://127.0.0.1:27017'
Register a connnection with the id, if omit, will add as default connection. All options exclude 'id' will direct pass to MongoDB::Connection. The host accept standard mongoDB uri scheme:
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/database]
More about, see http://www.mongodb.org/display/DOCS/Connections.
boot host => 'mongodb://127.0.0.1',db => 'test' # same as: add_connection host => 'mongodb://127.0.0.1', id => 'default'; use_connection; use_db 'test';
Boot is equivalent to call add_connection,use_connection,use_db.
# sandbox use_db 'test'; with_context { use_db 'tmp_db'; # now context db is 'tmp_db' ... }; # context db auto restor to 'test' # temp context with_context { context_collection->do_something; } connection => 'id2', db => 'test2', 'collection' => 'user'; # alternate style my $db2 = context_connection->get_database('test2'); with_context { # context db is $db2,collection is 'foo' print context_collection->count; } db => $db2, 'collection' => 'foo';
with_context let you create a temporary context(sandbox) to invoke the code block. Before execute the code block, current context will be saved, them build a temporary context to invoke the code, after code executed, saved context will be restored.
with_context
You can explicit setup the sandbox context include connection,db,collection, or just applied from parent container(context).
with_context allow nested, any with_context will build its context sanbox to run the attached code block.
use_db 'test'; with_context { # context db is 'db1' with_context { # context db is 'db2' } db => 'db2'; # context db restore to 'db1' } db => 'db1'; # context db restore to 'test'
with_context options key:
for_dbs { print context_db->name; } 'test1','test2','test3; for_dbs { print context_db->name; } context_connection->database_names;
Evaluates the code BLOCK for each database of the list. In block scope, context_db will switch to the list value, and $_ is alias of this context_db value.
for_connections { for_dbs { map { print $_ } context_db->collection_names } $_->database_names; } 'con_id1', 'con_id2'
Evaluates the code BLOCK against each connection of connection id list. In block scope, context_connection will switch to the list value, and $_ is alais of the current context_connection.
# print out all collection's count in the db for_collections { say $_->name, ' count:', db_count; } context_db->collection_names; # reindex some collections for_collections { db_re_index } 'foo','foo2','foo3'; # alternate for_collections { db_re_index } qw(foo foo2 foo3); # alternate for_collections { db_re_index } ('foo','foo2','foo3');
Github: http://github.com/nightsailer/mongo-x
MongoDB manual: http://www.mongodb.org/display/DOCS/Manual
Official MongoDB driver: MongoDB or http://github.com/mongodb/mongo-perl-driver
My fork: http://github.com/nightsailer/mongo-perl-driver
My blog about this project (Chinese only!): http://nightsailer.com/mongox
Pan Fan(nightsailer) <nightsailer at gmail dot com>
This software is copyright (c) 2010 by Pan Fan(nightsailer).
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install MongoX, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MongoX
CPAN shell
perl -MCPAN -e shell install MongoX
For more information on module installation, please visit the detailed CPAN module installation guide.