NAME

Mandel::Relationship::ListOf - A field points to many other MongoDB documents

DESCRIPTION

Mandel::Relationship::ListOf is a class used to describe the relationship where one document has a list of DBRefs that point to other documents. The connection between the documents is described in the database using DBRef.

This relationship is EXPERIMENTAL. Let me of you are using it or don't like it.

DATABASE STRUCTURE

A "person" that has list of "cats" will look like this in the database:

mongodb> db.persons.find();
{
  "_id" : ObjectId("5353ab13800fac3a0a8d5049"),
  "kittens" : [
    DBRef("cats", ObjectId("5353ab13c5483e16a1010000")),
    DBRef("cats", ObjectId("5353ab13c5483e16a1020000"))
  ]
}

mongodb> db.cats.find();
{ "_id" : ObjectId("5353ab13c5483e16a1010000") }
{ "_id" : ObjectId("5353ab13c5483e16a1020000") }

SYNOPSIS

Using DSL

package MyModel::Person;
use Mandel::Document;
list_of cats => 'MyModel::Cat';

Using object oriented interface

MyModel::Person->model->relationship(
  "list_of",
  "cats",
  "MyModel::Cat",
);

See also "relationship" in Mandel::Model.

Methods generated

# non-blocking
$person = MyModel::Person->new->push_cats($bson_oid, $pos, sub {
            my($person, $err, $cat_obj) = @_;
            # Note! This $cat_obj has only "id()" set
            # ...
          });

Add the $bson_oid to the "cats" list in $person.

$person = MyModel::Person->new->push_cats(\%constructor_args, $pos, sub {
            my($person, $err, $cat_obj) = @_;
            # ...
          });

Pushing a new cat with %constructor_args will also insert a new cat object into the database.

$person = MyModel::Person->new->push_cats($cat_obj, $pos, sub {
            my($person, $err, $cat_obj) = @_;
            # ...
          });

$pos is optional. When omitted, push_cats() will add the new element to the end of list. See http://docs.mongodb.org/manual/reference/operator/update/position/#up._S_position for details.

$person = MyModel::Cat->new->remove_cats($bson_oid, sub {
            my($self, $err) = @_;
            # Note! This $cat_obj has only "id()" set
          });

$person = MyModel::Cat->new->remove_cats($cat_obj, sub {
            my($self, $err) = @_;
            # ...
          });

Calling remove_cats() will only remove the link, and not the related object.

$person = MyModel::Cat->new->cats(sub {
            my($self, $err, $array_of_cats) = @_;
            # ...
          });

Retrieve all the related cat objects.

# blocking
$cat_obj = MyModel::Person->new->push_cats($bson_oid);
$cat_obj = MyModel::Person->new->push_cats(\%args);
$cat_obj = MyModel::Person->new->push_cats($cat_obj);
$person = MyModel::Person->new->remove_cats($bson_oid);
$person = MyModel::Person->new->remove_cats($cat_obj);
$array_of_cats = MyModel::Person->new->cats;

$cat_collection = MyModel::Person->new->search_cats;

Note! search() does not guaranty the order of the results, like cats() does.

ATTRIBUTES

Mandel::Relationship::ListOf inherits all attributes from Mandel::Relationship and implements the following new ones.

push_method_name

The name of the method used to add another document to the relationship.

remove_method_name

The name of the method used to remove an item from the list.

search_method_name

The name of the method used to search related documents.

METHODS

Mandel::Relationship::ListOf inherits all methods from Mandel::Relationship and implements the following new ones.

monkey_patch

Add methods to "document_class" in Mandel::Relationship.

SEE ALSO

Mojolicious, Mango, Mandel::Relationship

AUTHOR

Jan Henning Thorsen - jhthorsen@cpan.org