我想构造一个以下形式的查询:
select * from some_table
where
(field1 = 'x' or field2 = 'y')
and
(field3 = 'z' or field4 = 'w')
通过阅读文档,我认为它应该看起来像这样Mongoid
:
SomeTable.or({:field1 => 'x'}, {:field2 => 'y'})
.and # or is it .intersect?
.or({:field3 => 'z'}, {:field4 => 'w'})
但这不起作用 - mongo 选择器只是所有字段的“$or”。这样做的正确方法是什么?谢谢。
我也很欣赏相反的情况,例如 - 如何执行此查询:
select * from some_table
where
(field1 = 'x' and field2 = 'y')
or
(field3 = 'z' and field4 = 'w')
看来我需要结合使用mongo
and Mongoid
语法($something
操作员):
SomeTable.and( :$or => [ { :field1 => 'x' }, { :field2 => 'y' } ])
.and( :$or => [ { :field3 => 'z' }, { :field4 => 'w' } ])
其相反只是替换运算符:
SomeTable.or( :$and => [ { :field1 => 'x' }, { :field2 => 'y' } ])
.or( :$and => [ { :field3 => 'z' }, { :field4 => 'w' } ])
希望这对某人有帮助:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)