NAME
MongoDB::Database - A MongoDB Database
VERSION
version v0.708.3.0
SYNOPSIS
The MongoDB::Database class accesses to a database.
# accesses the foo database
my $db = $connection->foo;
You can also access databases with the "get_database($name)" in MongoDB::Connection method.
NAME
MongoDB::Database - A Mongo database
SEE ALSO
Core documentation on databases: http://dochub.mongodb.org/core/databases.
ATTRIBUTES
name
The name of the database.
METHODS
collection_names
my @collections = $database->collection_names;
Returns the list of collections in this database.
get_collection ($name)
my $collection = $database->get_collection('foo');
Returns a MongoDB::Collection for the collection called $name
within this database.
The coll
method is an alias for get_collection
.
get_gridfs ($prefix?)
my $grid = $database->get_gridfs;
Returns a MongoDB::GridFS for storing and retrieving files from the database. Default prefix is "fs", making $grid->files
"fs.files" and $grid->chunks
"fs.chunks".
See MongoDB::GridFS for more information.
drop
$database->drop;
Deletes the database.
last_error($options?)
my $err = $db->last_error({w => 2});
Finds out if the last database operation completed successfully. If the last operation did not complete successfully, returns a hash reference of information about the error that occurred.
The optional $options
parameter is a hash reference that can contain any of the following:
- w
-
Guarantees that the previous operation will be replicated to
w
servers before this command will return success. SeeMongoDB::Connection::w
for more information. - wtimeout
-
Milliseconds to wait for
w
copies of the data to be made. This parameter should generally be specified, as the database will otherwise wait forever ifw
copies cannot be made. - fsync
-
If true, behaves identically to
j
if journaling has been turned on formongod
.If
mongod
is not running with journaling, then this option requests that writes be immediatelysync
ed to disk if true.This option can not be used simultaneously with the
j
flag. - j
-
If true, the client will block until write operations have been committed to the server's journal. Prior to MongoDB 2.6, this option was ignored if the server was running without journaling. Starting with MongoDB 2.6, write operations will fail if this option is used when the server is running without journaling.
last_error
returns a hash with fields that vary, depending on what the previous operation was and if it succeeded or failed. If the last operation (before the last_error
call) failed, either:
If err
is null
and ok
is 1, the previous operation succeeded.
The fields in the hash returned can include (but are not limited to):
ok
-
This should almost be 1 (unless
last_error
itself failed). err
-
If this field is non-null, an error occurred on the previous operation. If this field is set, it will be a string describing the error that occurred.
code
-
If a database error occurred, the relevant error code will be passed back to the client.
errmsg
-
This field is set if something goes wrong with a database command. It is coupled with
ok
being 0. For example, ifw
is set and times out,errmsg
will be set to "timed out waiting for slaves" andok
will be 0. If this field is set, it will be a string describing the error that occurred. n
-
If the last operation was an update, upsert, or a remove, the number of objects affected will be returned.
wtimeout
-
If the previous option timed out waiting for replication.
waited
-
How long the operation waited before timing out.
wtime
-
If
w
was set and the operation succeeded, how long it took to replicate tow
servers. upserted
-
If an upsert occurred, this field will contain the new record's
_id
field. For upserts, either this field orupdatedExisting
will be present (unless an error occurred). updatedExisting
-
If an upsert updated an existing element, this field will be
true
. For upserts, either this field orupserted
will be present (unless an error occurred).
See "w" in MongoDB::Connection for more information.
run_command ($command)
my $result = $database->run_command([ some_command => 1 ]);
Runs a database command. The input should be an array reference of key-value pairs or a Tie::IxHash object with the command name as the first key. The use of a hash reference will only reliably work for commands without additional parameters.
It returns a string with the error message if the command fails. It returns the result of the command (a hash reference) on success. For a list of possible database commands, run:
my $commands = $db->run_command([listCommands => 1]);
There are a few examples of database commands in the "DATABASE COMMANDS" in MongoDB::Examples section.
See also core documentation on database commands: http://dochub.mongodb.org/core/commands.
eval ($code, $args?, $nolock?)
my $result = $database->eval('function(x) { return "hello, "+x; }', ["world"]);
Evaluate a JavaScript expression on the Mongo server. The $code
argument can be a string or an instance of MongoDB::Code. The $args
are an optional array of arguments to be passed to the $code
function. $nolock
(default false
) prevents the eval command from taking the global write lock before evaluating the JavaScript.
eval
is useful if you need to touch a lot of data lightly; in such a scenario the network transfer of the data could be a bottleneck. The $code
argument must be a JavaScript function. $args
is an array of parameters that will be passed to the function. $nolock
is a boolean value. For more examples of using eval see http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-Using{{db.eval%28%29}}.
AUTHORS
David Golden <david@mongodb.com>
Mike Friedman <friedo@friedo.com>
Kristina Chodorow <k.chodorow@gmail.com>
Florian Ragwitz <rafl@debian.org>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2015 by MongoDB, Inc..
This is free software, licensed under:
The Apache License, Version 2.0, January 2004