Mongoid:检索所有嵌入的文档(Mongoid: Retrieves all embedded documents)
假设我们有这些模型:
class Person include Mongoid::Document embeds_many :albums end class Album include Mongoid::Document embeds_many :photos end class Photo include Mongoid::Document end
我想要的是检索特定
Person
所有Photo
。 是否存在mongoid / mongodb快捷方式或唯一的方法是迭代person.albums并将所有album.photos
存储在新阵列中?谢谢。
Suppose we have these models:
class Person include Mongoid::Document embeds_many :albums end class Album include Mongoid::Document embeds_many :photos end class Photo include Mongoid::Document end
What I want is to retrieves all
Photo
of a particularPerson
. Is there a mongoid/mongodb shortcuts or the only way is to iterate overperson.albums
and store allalbum.photos
in a new array?Thanks.
原文:https://stackoverflow.com/questions/18421897
最满意答案
你有什么应该工作, 但是 :不要使用
qx
。 这是古老而危险的; 无论你向它提供什么都通过shell,所以如果/bin/sh
不是你/bin/sh
那么很容易受到shell注入或者遇到意外。使用
open()
的多参数形式,它完全绕过了shell。open my $fh, '-|', 'df', "-$opt", '/tmp' or die "Can't open pipe: $!"; my @lines = <$fh>; # or read in a loop, which is more likely what you want close $fh or die "Can't close pipe: $!";
What you have should work, but: don't ever use
qx
. It's ancient and dangerous; whatever you feed to it goes through the shell, so it's very easy to be vulnerable to shell injection or run into surprises if/bin/sh
isn't quite what you expected.Use the multi-arg form of
open()
, which bypasses the shell entirely.open my $fh, '-|', 'df', "-$opt", '/tmp' or die "Can't open pipe: $!"; my @lines = <$fh>; # or read in a loop, which is more likely what you want close $fh or die "Can't close pipe: $!";
相关问答
更多-
超时通常使用警报完成。 sub _usb_device { # Scope array my @array; # Try shell command eval { local $SIG{ALRM} = sub { die "timeout\n" }; local $/ = ""; alarm 10; @array = qx( hwinfo --usb ); alarm 0; ...
-
这个答案是一个非常天真的方法。 它很容易陷入僵局。 不要使用它! ikegami在下面的评论中解释道 : 如果父节点足够写入连接到子节点的STDIN的管道,并且在从STDIN读取STDIN之前,如果子节点输出足够的连接到STDOUT的管道,则会出现死锁。 (在某些系统上,这可能只有4KB)。使用类似select,线程等的解决方案。更好的解决方案是使用已经解决了问题的工具(IPC :: Run3或IPC ::跑)。 IPC :: Open2和IPC :: Open3太低级别,在大多数情况下都不适用 我会留下原 ...
-
perl - “tee”qx操作符(perl - “tee” the qx operator)[2023-06-09]
您可以在打印行时自行打开手柄并手动填充阵列。 像这样的东西可能会工作, open my $fh, '-|', $some_command; while(<$fh>) { print $_; push @files, $_; } close $fh; You could open the handle and manually populate the array yourself while printing the lines as you go. Something ... -
要直接回答这个问题,可以使用perlref中描述的字符串中的interpolate-arbitrary-expression-in-string成语: print qx{echo @{[ __FILE__ ]}}; 评论中提出了一些注意事项: []提供列表上下文。 这并不重要,但总体上值得注意。 如果__FILE__具有shell元字符,可能会遇到麻烦。 如果您的目标是在解决所有符号链接的情况下查找perl脚本的路径,则可能需要查看FindBin模块 。 一段时间以来, FindBin一直是核心perl发 ...
-
所有进程都从另一个进程派生(init除外)。 您可以通过使用POSIX的isatty函数来判断程序是从open , qx// , open2还是open3运行,但是没有好的方法来确定是否在不查看进程树的情况下由system运行,即使这样它可能会变得模糊(例如system "nohup", "./foo.pl"将不会将调用perl进程作为其父进程)。 All processes are forked from another process (except init). You can sort of tel ...
-
你可以在Perl中使用正则表达式匹配。 无需调用外部“grep”命令。 $var =~ /foo/; 请阅读文档perlrequick ,了解如何在变量中搜索模式。 同样令人感兴趣的是Perl自己的grep 。 $var="foobar"; if ( $var =~ /foo/){ print "found foo\n"; } you can just use regex matching in Perl. No need to call external "grep" command. $var ...
-
是。 右侧括号()中的每个逗号分隔项都是术语 。 它们在Perl中具有最高优先级,并从左到右进行评估。 反引号是类似引用的运算符 。 Yes. Each of the comma-separated items in your brackets () on the right-hand side are terms. These have the highest precedence in Perl, and evaluate left-to-right. Backticks are quote-like ...
-
您可以使用grep过滤qx输出: my @output = grep {/^(Up|Down)load:/} qx"/home/user/.local/bin/speedtest-cli"; print $fh $_ for @output; 作为旁注,您可以使用POSIX简化$ timer代码: use POSIX qw(strftime); my @localtime = localtime; my $nice_timestamp = strftime('%Y%m%d', @localtime); ...
-
你有什么应该工作, 但是 :不要使用qx 。 这是古老而危险的; 无论你向它提供什么都通过shell,所以如果/bin/sh不是你/bin/sh那么很容易受到shell注入或者遇到意外。 使用open()的多参数形式,它完全绕过了shell。 open my $fh, '-|', 'df', "-$opt", '/tmp' or die "Can't open pipe: $!"; my @lines = <$fh>; # or read in a loop, which is more likely w ...
-
将参数作为数组传递,就像使用任何其他程序一样(Perl脚本不是特殊的;它是Perl脚本是一个实现细节): system($^X, "$Bin/GetResults.pl", "-templatefile", "$template"); 您可以在数组中排列所有内容并使用它: my @args = ("$Bin/GetResults.pl", "-templatefile", "$template"); system($^X, @args); 甚至可以将@ $^X添加到@args 。 等等。 Pass th ...