2
我用猫鼬设置如下(简化和压缩)的数据模型:如何调用所有()上的猫鼬::加入对象
package Model::Tag;
use Moose;
use Mongoose::Class; with 'Mongoose::Document';
has 'value' => (is => 'rw', isa => 'Str', required => 1);
no Moose;
__PACKAGE__->meta->make_immutable;
package Model::Document;
use Moose;
use Mongoose::Class; with 'Mongoose::Document';
has 'title' => (is => 'rw', isa => 'Str', required => 1);
has_many 'tags' => (is => 'rw', isa => 'Model::Tag');
no Moose;
__PACKAGE__->meta->make_immutable;
我测试的重要部分是:
package main;
use strict;
use warnings;
use Data::Dumper;
my $expected; my $got;
my $doc = Model::Document->new(title => 'My new document with many tags');
my $tag1 = Model::Tag->new(value => 'foo');
my $tag2 = Model::Tag->new(value => 'bar');
my $x1 = $doc->tags->add($tag1);
my $x2 = $doc->tags->add($tag2);
# print "x1 = $x1 x2 = $x2 \n";
my $document_tags = $doc->tags;
print Dumper $document_tags;
can_ok($document_tags, 'all');
my $tag_array_ref = $document_tags->all();
现在的问题:
$ document_tags的倾倒输出是猫鼬::加入对象:
$VAR1 = bless({
'delete_buffer' => {},
'with_class' => 'Model::Tag',
'buffer' => {
'58102804' => bless({
'value' => 'foo'
}, 'Model::Tag'),
'58069732' => bless({
'value' => 'bar'
}, 'Model::Tag')
}
}, 'Mongoose::Join');
和文档有关Mongoose::Join列表的方法:
add, remove, find, find_one, first, all, hash_on, hash_array, query, collection, with_collection_name
但调用
$document_tags->all();
导致错误
Can't use an undefined value as a HASH reference at C:/Perl/site/lib/Mongoose.pm line 132.
问题是什么?
在此先感谢您的帮助和意见。
我知道如何将值存储在Mongodb中。但我没想到我必须存储这些值才能够再次读取它们。这很奇怪 – smartmeta 2013-04-03 08:07:30
它看起来像从'Mongoose :: Join'文档的字面解读 - 'all'是'find'的包装器,它依次查询MongoDB。它不检查由'add'添加的未保存文档的缓冲区;对于猫鼬维护者来说是否应该是一个问题。 – 2013-04-04 04:43:18
是的,但维护人员建议在Stackoverflow上进行询问。 :-( – smartmeta 2013-04-05 07:11:39