MongoX 介绍
MongoX 是 Perl 语言用来操作 MongoDB 数据库的开发包。
最快速模式,连接到127.0.0.1,默认数据库 test
use MongoX host => ‘mongodb://127.0.0.1′,db => ‘test’;
按部就班的来
use MongoX;
添加一个默认的connection
add_connection host => ‘127.0.0.1′;
启用默认的connection
use_connection;
切换到数据库 test
use_db ‘test’;
添加另一个数据库连接,连接id为remote2
add_connection host => ‘192.168.1.1′,id => ‘remote2′;
切换到remote2 连接
use_connection ‘remote2′;
返回当前数据库中 foo这个collection
my $foo = get_collection ‘foo’;
将 ‘foo’ 设置为默认的collection
use_collection ‘foo’;
context_collection是默认collection
say ‘total rows:’,context_collection->count();
my $id = context_collection->insert({ name => ‘Pan’, home => ‘Beijing’ });
context_db是当前默认的数据库,即 remote2连接中的foo
my $gridfs = context_db->get_gridfs;