---
name: "Mango::Database"
section: "3pm"
platform: "linux"
package: "libmango-perl"
version: "1.30-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/Mango::Database"
---

# Mango::Database(3pm)

MongoDB database

## NAME

Mango::Database - MongoDB database

## SYNOPSIS

```
  use Mango::Database;
  my $db = Mango::Database->new(mango => $mango);
  my $collection = $db->collection('foo');
  my $gridfs     = $db->gridfs;
```

## DESCRIPTION

Mango::Database is a container for MongoDB databases used by Mango.

## ATTRIBUTES

Mango::Database implements the following attributes.

### mango

```
  my $mango = $db->mango;
  $db       = $db->mango(Mango->new);
```

Mango object this database belongs to. Note that this reference is usually weakened, so the Mango object needs to be referenced elsewhere as well.

### name

```
  my $name = $db->name;
  $db      = $db->name('bar');
```

Name of this database.

## METHODS

Mango::Database inherits all methods from Mojo::Base and implements the following new ones.

### build\_write\_concern

```
  my $concern = $db->build_write_concern;
```

Build write concern based on l\</"mango"\> settings.

### collection

```
  my $collection = $db->collection('foo');
```

Build Mango::Collection object for collection.

### collection\_names

```
  my $names = $db->collection_names;
```

Names of all collections in this database. You can filter the results by using the same arguments as for `"list_collections"`. You can also append a callback to perform operation non-blocking.

```
  $db->collection_names(sub {
    my ($db, $err, $names) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
```

### command

```
  my $doc = $db->command(bson_doc(text => 'foo.bar', search => 'test'));
  my $doc = $db->command(bson_doc(getLastError => 1, w => 2));
  my $doc = $db->command('getLastError', w => 2);
```

Run command against database. You can also append a callback to run command non-blocking.

```
  $db->command(('getLastError', w => 2) => sub {
    my ($db, $err, $doc) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
```

### dereference

```
  my $doc = $db->dereference($dbref);
```

Resolve database reference. You can also append a callback to perform operation non-blocking.

```
  $db->dereference($dbref => sub {
    my ($db, $err, $doc) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
```

### gridfs

```
  my $gridfs = $db->gridfs;
```

Build Mango::GridFS object.

### list\_collections

```
  # return a cursor for all collections
  my $cursor = $db->list_collections;
  # only collections which name matchs a regex
  my $cursor = $db->list_collections(filter => { name => qr{^prefix} });
  # only capped collections
  my $cursor = $db->list_collections(filter => { 'options.capped' => 1 });
  # only the first 10 collections
  my $cursor = $db->list_collections(cursor => { batchSize => 10 });
```

Returns a Mango::Cursor of all collections in this database. Each collection is represented by a document containing at least the keys `"name"` and `"options"`. You can also append a callback to perform operation non-blocking.

```
  $db->list_collections(sub {
    my ($db, $err, $cursor) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
```

### stats

```
  my $stats = $db->stats;
```

Get database statistics. You can also append a callback to perform operation non-blocking.

```
  $db->stats(sub {
    my ($db, $err, $stats) = @_;
    ...
  });
  Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
```

## SEE ALSO

Mango, Mojolicious::Guides, \<http://mojolicio.us\>.
