关于链接多个any_of条件的问题#Mongoid
我有一个要求,我需要运行如下所示的 MongoDB 查询:
db.collection.find({ $or : [{"field1" : "value1"}, {"field2" : "value2"}], $or : [{"field3" : "value3"}, {"field4" : "value4"}]})
即
(field1 == value 1 or field2 == value2) and (field3 == value3 or field4 == value4)
我想通过条件链接来实现此目的,因为查询已形成 动态地从代码的不同部分进行。但是,如果我尝试执行类似以下操作,
criteria = Collection.any_of({"field1" => "value1"}, {"field2" => "value2"}) criteria = criteria.any_of({"field3" => "value3"}, {"field4" => "value4"})
我会得到结果查询,其中所有这些都组合成一个 $or 像这样的语句
db.collection.find({ $or : [{"field1" : "value1"}, {"field2" : "value2"}, {"field3" : "value3"}, {"field4" : "value4"}]})
使用标准链接实现两个“any_of”的“and”的方法是什么?
I have a requirement where I need to run a MongoDB query like the following:
db.collection.find({ $or : [{"field1" : "value1"}, {"field2" : "value2"}], $or : [{"field3" : "value3"}, {"field4" : "value4"}]})
i.e.
(field1 == value 1 or field2 == value2) and (field3 == value3 or field4 == value4)
I want to achieve this through criteria chaining because the query gets formed
dynamically from different parts of the code. But if I try doing something like the following
criteria = Collection.any_of({"field1" => "value1"}, {"field2" => "value2"}) criteria = criteria.any_of({"field3" => "value3"}, {"field4" => "value4"})
I get the resultant query where all these are combined into a single $or
statement like
db.collection.find({ $or : [{"field1" : "value1"}, {"field2" : "value2"}, {"field3" : "value3"}, {"field4" : "value4"}]})
What is the way to achieve "and" of the two "any_of" using criteria chaining?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以通过避免any_of来做到这一点。
You can do it with avoid any_of.
你可以用 mongoid 2.4.0 编写:
you can write this with mongoid 2.4.0:
对于 Mongoid 3 和 4,有一种相对较好的链接方式或标准,无需合并。
TL;DR
Mongoid 维护者 Durran 在这个 Github 问题中展示了上述技巧:
https://github.com/mongoid/mongoid/issues/2170
可以任意上链标准使用相同的技术和一些数组技巧,如下所示:
每行将添加另一个 and 条件。
还值得注意的是,您不必从模型构建每个选择器。您可以从权限系统或其他东西中获取初始范围,然后在添加更多条件之前只需调用 .selector 即可。
For Mongoid 3 and 4 there is a relatively nice way of chaining or criteria without merging.
TL;DR
Mongoid maintainer Durran showed the above trick in this Github issue:
https://github.com/mongoid/mongoid/issues/2170
You can arbitrarily chain criteria using the same technique and some array trickery like this:
Each row will add another and condition.
It may be worth noting too that you do not have to build each selector from the model. You can get an initial scope back from your permission system or something ans just call .selector on it before adding on more criteria.
我花了很长时间才做到这一点,但这对我有用:
您必须确保将
$or
条件的单独条件包装在{}
中,以便它正确地嵌套了 OR。 (使用 Mongoid3/Moped)It took me forever to get this right, but this is what worked for me:
You have to make sure to wrap the individual criteria for your
$or
condition in{}
so that it nests the OR properly. (Using Mongoid3/Moped)现在仍然如此。我只能直接使用 Ruby Mongo Driver API 来解决这个问题。
This is still the case. I was able to work around this only by using the Ruby Mongo Driver API directly.