Mandel::Relationship::ListOf - A field points to many other MongoDB documents
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.
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") }
package MyModel::Person; use Mandel::Document; list_of cats => 'MyModel::Cat';
MyModel::Person->model->relationship( "list_of", "cats", "MyModel::Cat", );
See also "relationship" in Mandel::Model.
# 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.
$bson_oid
$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.
%constructor_args
$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.
$pos
push_cats()
$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.
remove_cats()
$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.
search()
cats()
Mandel::Relationship::ListOf inherits all attributes from Mandel::Relationship and implements the following new ones.
The name of the method used to add another document to the relationship.
The name of the method used to remove an item from the list.
The name of the method used to search related documents.
Mandel::Relationship::ListOf inherits all methods from Mandel::Relationship and implements the following new ones.
Add methods to "document_class" in Mandel::Relationship.
Mojolicious, Mango, Mandel::Relationship
Jan Henning Thorsen - jhthorsen@cpan.org
jhthorsen@cpan.org
To install Mandel, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mandel
CPAN shell
perl -MCPAN -e shell install Mandel
For more information on module installation, please visit the detailed CPAN module installation guide.