/usr/lib/perl5/MongoDB/Database.pm is in libmongodb-perl 0.702.1+ds-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | #
# Copyright 2009-2013 10gen, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
package MongoDB::Database;
{
$MongoDB::Database::VERSION = '0.702.1';
}
# ABSTRACT: A MongoDB Database
use Moose;
use MongoDB::GridFS;
use Carp 'carp';
has _client => (
is => 'ro',
isa => 'MongoDB::MongoClient',
required => 1,
);
has name => (
is => 'ro',
isa => 'Str',
required => 1,
);
sub AUTOLOAD {
my $self = shift @_;
our $AUTOLOAD;
my $coll = $AUTOLOAD;
$coll =~ s/.*:://;
carp sprintf q{AUTOLOADed collection method names are deprecated and will be removed in a future release. Use $db->get_collection( '%s' ) instead.}, $coll;
return $self->get_collection($coll);
}
sub collection_names {
my ($self) = @_;
my $it = $self->get_collection('system.namespaces')->query({});
return grep {
not ( index( $_, '$' ) >= 0 && index( $_, '.oplog.$' ) < 0 )
} map {
substr $_->{name}, length( $self->name ) + 1
} $it->all;
}
sub get_collection {
my ($self, $collection_name) = @_;
return MongoDB::Collection->new(
_database => $self,
name => $collection_name,
);
}
sub get_gridfs {
my ($self, $prefix) = @_;
$prefix = "fs" unless $prefix;
return MongoDB::GridFS->new(
_database => $self,
prefix => $prefix
);
}
sub drop {
my ($self) = @_;
return $self->run_command({ 'dropDatabase' => 1 });
}
sub last_error {
my ($self, $options) = @_;
my $cmd = Tie::IxHash->new("getlasterror" => 1);
if ($options) {
$cmd->Push("w", $options->{w}) if $options->{w};
$cmd->Push("wtimeout", $options->{wtimeout}) if $options->{wtimeout};
$cmd->Push("fsync", $options->{fsync}) if $options->{fsync};
$cmd->Push("j", 1) if $options->{j};
}
return $self->run_command($cmd);
}
sub run_command {
my ($self, $command) = @_;
my $obj = $self->get_collection('$cmd')->find_one($command);
return $obj if $obj->{ok};
$obj->{'errmsg'};
}
sub eval {
my ($self, $code, $args) = @_;
my $cmd = tie(my %hash, 'Tie::IxHash');
%hash = ('$eval' => $code,
'args' => $args);
my $result = $self->run_command($cmd);
if (ref $result eq 'HASH' && exists $result->{'retval'}) {
return $result->{'retval'};
}
else {
return $result;
}
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=head1 NAME
MongoDB::Database - A MongoDB Database
=head1 VERSION
version 0.702.1
=head1 SYNOPSIS
The MongoDB::Database class accesses to a database.
# accesses the foo database
my $db = $connection->foo;
You can also access databases with the L<MongoDB::Connection/"get_database($name)">
method.
=head1 NAME
MongoDB::Database - A Mongo database
=head1 SEE ALSO
Core documentation on databases: L<http://dochub.mongodb.org/core/databases>.
=head1 ATTRIBUTES
=head2 name
The name of the database.
=head1 METHODS
=head2 collection_names
my @collections = $database->collection_names;
Returns the list of collections in this database.
=head2 get_collection ($name)
my $collection = $database->get_collection('foo');
Returns a L<MongoDB::Collection> for the collection called C<$name> within this
database.
=head2 get_gridfs ($prefix?)
my $grid = $database->get_gridfs;
Returns a L<MongoDB::GridFS> for storing and retrieving files from the database.
Default prefix is "fs", making C<$grid-E<gt>files> "fs.files" and C<$grid-E<gt>chunks>
"fs.chunks".
See L<MongoDB::GridFS> for more information.
=head2 drop
$database->drop;
Deletes the database.
=head2 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 occured.
The optional C<$options> parameter is a hash reference that can contain any of
the following:
=over 4
=item w
Guarantees that the previous operation will be replicated to C<w> servers before
this command will return success. See C<MongoDB::Connection::w> for more
information.
=item wtimeout
Milliseconds to wait for C<w> copies of the data to be made. This parameter
should generally be specified, as the database will otherwise wait forever if
C<w> copies cannot be made.
=item fsync
If true, the database will fsync to disk before returning.
=item j
If true, awaits the journal commit before returning. If the server is running without journaling, it returns immediately, and successfully.
=back
C<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 C<last_error> call) failed, either:
=over 4
=item C<err> will be set or
=item C<errmsg> will be set and C<ok> will be 0.
=back
If C<err> is C<null> and C<ok> is 1, the previous operation succeeded.
The fields in the hash returned can include (but are not limited to):
=over 4
=item C<ok>
This should almost be 1 (unless C<last_error> itself failed).
=item C<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.
=item C<code>
If a database error occurred, the relevant error code will be passed back to the
client.
=item C<errmsg>
This field is set if something goes wrong with a database command. It is
coupled with C<ok> being 0. For example, if C<w> is set and times out,
C<errmsg> will be set to "timed out waiting for slaves" and C<ok> will be 0. If
this field is set, it will be a string describing the error that occurred.
=item C<n>
If the last operation was an update, upsert, or a remove, the number of
objects affected will be returned.
=item C<wtimeout>
If the previous option timed out waiting for replication.
=item C<waited>
How long the operation waited before timing out.
=item C<wtime>
If C<w> was set and the operation succeeded, how long it took to replicate to
C<w> servers.
=item C<upserted>
If an upsert occured, this field will contain the new record's C<_id> field. For
upserts, either this field or C<updatedExisting> will be present (unless an
error occurred).
=item C<updatedExisting>
If an upsert updated an existing element, this field will be C<true>. For
upserts, either this field or C<upserted> will be present (unless an error
occurred).
=back
See L<MongoDB::Connection/w> for more information.
=head2 run_command ($command)
my $result = $database->run_command({ some_command => 1 });
Runs a database command. Returns a string with the error message if the
command fails. 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
L<MongoDB::Examples/"DATABASE COMMANDS"> section.
See also core documentation on database commands:
L<http://dochub.mongodb.org/core/commands>.
=head2 eval ($code, $args?)
my $result = $database->eval('function(x) { return "hello, "+x; }', ["world"]);
Evaluate a JavaScript expression on the Mongo server. The C<$code> argument can
be a string or an instance of L<MongoDB::Code>. The C<$args> are an optional
array of arguments to be passed to the C<$code> function.
C<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 C<$code> argument
must be a JavaScript function. C<$args> is an array of parameters that will be
passed to the function. For more examples of using eval see
L<http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-Using{{db.eval%28%29}}>.
=head1 AUTHORS
=over 4
=item *
Florian Ragwitz <rafl@debian.org>
=item *
Kristina Chodorow <kristina@mongodb.org>
=item *
Mike Friedman <mike.friedman@10gen.com>
=back
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2013 by 10gen, Inc..
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
=cut
|