首页 \ 问答 \ Mongoid:检索所有嵌入的文档(Mongoid: Retrieves all embedded documents)

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 particular Person. Is there a mongoid/mongodb shortcuts or the only way is to iterate over person.albums and store all album.photos in a new array?

Thanks.


原文:https://stackoverflow.com/questions/18421897
更新时间:2022-02-16 17:02

最满意答案

你有什么应该工作, 但是 :不要使用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太低级别,在大多数情况下都不适用 我会留下原 ...
  • 您可以在打印行时自行打开手柄并手动填充阵列。 像这样的东西可能会工作, 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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)