[英]How to find whether there is a 'child' doc. in MongoDBx::Class
I am learning the Perl MongoDBX::Class and writing a blog app.我正在学习 Perl MongoDBX::Class 并编写博客应用程序。
Bellow are Post and Comment model.波纹管是 model 的帖子和评论。 If call this method:如果调用此方法:
my $comments = $a_post->comments;
it halt the application if there is no comment for this post.如果没有对此帖子的评论,它将停止应用程序。 The question is how to check whether this post has comment?问题是如何检查这篇文章是否有评论?
Thanks.谢谢。
package Model::Schema::Post;
use MongoDBx::Class::Moose;
use namespace::autoclean;
with 'MongoDBx::Class::Document';
has 'title' => (is => 'rw', isa => 'Str', required => 1,);
belongs_to 'author' => (is => 'ro', isa => 'Author', required => 1);
has 'post_date' => (is => 'ro', isa => 'DateTime', traits => ['Parsed'], required => 1);
has 'text' => (is => 'rw', isa => 'Str', required => 1);
joins_many 'comments' => (is => 'ro', isa => 'Comment', coll => 'comments', ref => 'post');
holds_many 'tags' => (is => 'rw', isa => 'Tag', predicate => 'has_tag');
__PACKAGE__->meta->make_immutable;
package Model::Schema::Comment;
use MongoDBx::Class::Moose;
use namespace::autoclean;
with 'MongoDBx::Class::Document';
belongs_to 'post' => (is => 'ro', isa => 'Post', required => 1);
has 'author' => (is => 'ro', isa => 'Author', required => 1);
has 'comment_date' => (is => 'ro', isa => 'DateTime', traits => ['Parsed'], required => 1);
has 'text' => (is => 'rw', isa => 'Str', required => 1);
has 'rateing' => (is => 'rw', isa => 'Int');
__PACKAGE__->meta->make_immutable;
I'm not sure why your application halts.我不确定您的应用程序为什么会停止。 Looking at your schema, running查看您的架构,运行
my $comments = $a_post->comments;
Should return a MongoDBx::Class::Cursor object (which is really just a MongoDB::Cursor object). Should return a MongoDBx::Class::Cursor object (which is really just a MongoDB::Cursor object). On this cursor, you can run:在这个 cursor 上,您可以运行:
my $num_comments = $comments->count;
if ($num_comments > 0) {
my @comments = $comments->all;
...
}
If this doesn't help you, and your application still hangs, It'll help me if you send me a sample of your code so I can try to find out what's going on.如果这对您没有帮助,并且您的应用程序仍然挂起,如果您向我发送您的代码示例,这将对我有所帮助,以便我可以尝试找出发生了什么。
By the way I'm the author of MongoDBx::Class.顺便说一句,我是 MongoDBx::Class 的作者。
PS If you feel you've stumbled upon some bug in MongoDBx::Class, feel free to open a bug report as described on the MongoDBx::Class page on CPAN. PS 如果你觉得你偶然发现了 MongoDBx::Class 中的一些错误,请随时按照 CPAN 上的 MongoDBx::Class 页面上的说明打开错误报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.