关于链接多个any_of条件的问题#Mongoid

发布于 2024-10-12 21:54:56 字数 751 浏览 3 评论 0原文

我有一个要求,我需要运行如下所示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深爱不及久伴 2024-10-19 21:54:56

你可以通过避免any_of来做到这一点。

criteria = Collection.where('$or' => [{"field1" => "value1"}, {"field2" => "value2"}])
criteria = criteria.where('$or' => [{"field3" => "value3"}, {"field4" => "value4"}])

You can do it with avoid any_of.

criteria = Collection.where('$or' => [{"field1" => "value1"}, {"field2" => "value2"}])
criteria = criteria.where('$or' => [{"field3" => "value3"}, {"field4" => "value4"}])
只是偏爱你 2024-10-19 21:54:56

你可以用 mongoid 2.4.0 编写:

  Collection.all_of({"$or" => [{"field1" => "value1"}, {"field2" => "value2"}]}, {"$or" => [{"field3" => "value3"}, {"field4" => "value4"}]})

you can write this with mongoid 2.4.0:

  Collection.all_of({"$or" => [{"field1" => "value1"}, {"field2" => "value2"}]}, {"$or" => [{"field3" => "value3"}, {"field4" => "value4"}]})
你怎么这么可爱啊 2024-10-19 21:54:56

对于 Mongoid 3 和 4,有一种相对较好的链接方式或标准,无需合并。

TL;DR

MyModel.all_of(MyModel.xxx.selector, MyModel.yyy.selector)

Mongoid 维护者 Durran 在这个 Github 问题中展示了上述技巧:
https://github.com/mongoid/mongoid/issues/2170

可以任意上链标准使用相同的技术和一些数组技巧,如下所示:

selectors = []
selectors << MyModel.search(term) if term
selectors << MyModel.some_any_of if some_condition
selectors << MyModel.some_other_any_of if some_other_condition
...
MyMode..all_of(*selectors)

每行将添加另一个 and 条件。

还值得注意的是,您不必从模型构建每个选择器。您可以从权限系统或其他东西中获取初始范围,然后在添加更多条件之前只需调用 .selector 即可。

For Mongoid 3 and 4 there is a relatively nice way of chaining or criteria without merging.

TL;DR

MyModel.all_of(MyModel.xxx.selector, MyModel.yyy.selector)

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:

selectors = []
selectors << MyModel.search(term) if term
selectors << MyModel.some_any_of if some_condition
selectors << MyModel.some_other_any_of if some_other_condition
...
MyMode..all_of(*selectors)

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.

春夜浅 2024-10-19 21:54:56

我花了很长时间才做到这一点,但这对我有用:

scope :for_user, lambda {|user| 
    any_of(
      {recipient_id: Moped::BSON::ObjectId.from_string(user.id)}, 
      {sender_id: Moped::BSON::ObjectId.from_string(user.id)},
      {sender_email: user.email},
      {recipient_email: user.email}
    )
  }

您必须确保将 $or 条件的单独条件包装在 {} 中,以便它正确地嵌套了 OR。 (使用 Mongoid3/Moped)

It took me forever to get this right, but this is what worked for me:

scope :for_user, lambda {|user| 
    any_of(
      {recipient_id: Moped::BSON::ObjectId.from_string(user.id)}, 
      {sender_id: Moped::BSON::ObjectId.from_string(user.id)},
      {sender_email: user.email},
      {recipient_email: user.email}
    )
  }

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)

心意如水 2024-10-19 21:54:56

现在仍然如此。我只能直接使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文