The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MongoDB::Simple

SYNOPSIS

package My::Data::Class;
use base 'MongoDB::Simple';
use MongoDB::Simple;

database 'dbname';
collection 'collname';

string 'stringfield' => {
    "changed" => sub {
        my ($self, $value) = @_;
        # ... called when changes to 'stringfield' are saved in database
    }
};
date 'datefield';
boolean 'booleanfield';
object 'objectfield';
array 'arrayfield';
object 'typedobject' => { type => 'My::Data::Class::Foo' };
array 'typedarray' => { type => 'My::Data::Class::Bar' };
array 'multiarray' => { types => ['My::Data::Class::Foo', 'My::Data::Class::Bar'] };

package My::Data::Class::Foo;

parent type => 'My::Data::Class', key => 'typedobject';

matches sub {
    my ($doc) = @_;
    my %keys = map { $_ => 1 } keys %$doc;
    return 1 if (scalar keys %keys == 1) && $keys{fooname};
    return 0;
}

string 'fooname';

package My::Data::Class::Bar;

parent type => 'My::Data::Class', key => 'typedarray';

matches sub {
    my ($doc) = @_;
    my %keys = map { $_ => 1 } keys %$doc;
    return 1 if (scalar keys %keys == 1) && $keys{barname};
    return 0;
}

string 'barname';

package main;

use MongoDB;
use DateTime;

my $mongo = new MongoClient;
my $cls = new My::Data::Class(client => $mongo);

$cls->stringfield("Example string");
$cls->datefield(DateTime->now);
$cls->booleanfield(true);
$cls->objectfield({ foo => "bar" });
push $cls->arrayfield, 'baz';

$cls->typedobject(new My::Data::Class::Foo);
$cls->typedobject->fooname('Foo');

my $bar = new My::Data::Class::Bar;
$bar->barname('Bar');
push $cls->typedarray, $bar;

my $id = $cls->save;

my $cls2 = new My::Data::Class(client => $mongo);
$cls2->load($id);

DESCRIPTION

MongoDB::Simple simplifies mapping of MongoDB documents to Perl objects.

SEE ALSO

Documentation needs more work - refer to the examples in the t/test.t file.

AUTHORS

Ian Kent - <iankent@cpan.org> - original author

COPYRIGHT AND LICENSE

This library is free software under the same terms as perl itself

Copyright (c) 2013 Ian Kent

MongoDB::Simple is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.