如何找到其属性foo或bar等于查询值的对象? [英] How do I find an object whose properties foo or bar equal a query value?

查看:68
本文介绍了如何找到其属性foo或bar等于查询值的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了google并查看了文档,但没有找到任何示例.

I searched google and looked at the docs, but I found no example of this.

我有这个带有嵌入式子对象的对象:

I have this object with embedded children:

{
  teacher: "Balthazar",
  students: [
    { name: 'Wilfred', sibling: 'Cordelia' },
    { name: 'Mortimer', sibling: 'Arthur' },
    { name: 'Cordelia', sibling: 'Wilfred' },
    { name: 'Arthur' sibling: 'Mortimer' },
  ]
}

我想查找namesibling['Wilfred', 'Cordelia', 'foo']中的Balthazar个学生.我知道它是这样工作的:

I want to find Balthazar's students whose name or sibling is in ['Wilfred', 'Cordelia', 'foo']. I got it to work this way:

criteria_a = balthazar.students.in(name: ['Wilfred', 'Cordelia'])
criteria_b = balthazar.students.in(sibling: ['Wilfred', 'Cordelia'])
criteria_a << criteria_b

但是,有没有更清洁的方法来链接标准?我尝试了链接,但是没有用.它会产生一个AND交集,而不是一个OR.

But is there a cleaner way to chain criteria? I tried chaining, but it doesn't work. It produces an AND intersection, not an OR.

balthazar.students.
  .in(name: ['Wilfred', 'Corderial'])
  .in(sibling: ['Wilfred', 'Corderial'])

因此,简单来说,此查询为name is in array **AND** sibling is in array.但我要name is in array __OR__ sibling is in array.

So, in plain speak, this query is name is in array **AND** sibling is in array. But I want name is in array __OR__ sibling is in array.

推荐答案

您可以使用any_of

balthazar.students.any_of({name: ['Wilfred', 'Cordelia']}, {sibling: ['Wilfred', 'Cordelia']})

这篇关于如何找到其属性foo或bar等于查询值的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆