如何用any_of和any_in查询ruby中的MongoID?

时间:2018-02-13 05:05:27

标签: ruby mongodb mongoid

我有一些看起来像这样的请求。

Request: 1
assigned_id: xyz
assigned_2_id: nil
check_number: 1234
check_number_helper: nil

Request: 2
assigned_id: abc
assigned_2_id: xyz
check_number: 3344
check_number_helper: 1234

Request: 3
assigned_id: xyz
assigned_2_id: ffg
check_number: 1234
check_number_helper: 6678

Request: 4
assigned_id: xyz
assigned_2_id: ffg
check_number: 7777
check_number_helper: 6678

我需要找到每个请求,其中assigned_id或assigned_2_id是xyz AND check_number或check_number_helper是1234

我尝试了以下操作,但它返回所有匹配的assigned_id,assigned_2_id,check_number和check_number_helper(因此在上面的数据中返回所有请求),因为它匹配至少一个字段。

Request.any_of({:assigned_2_id => assigned_id.to_s}, {:assigned_id => assigned_id.to_s}).any_of({:check_number => check_num}, {:check_number_helper => check_num}).asc(:timestamp_requested)

1 个答案:

答案 0 :(得分:0)

可能不是您要找的答案。但关闭了我能找到的问题。这是我能想到的唯一解决方案(所以任何人都可以得到一些帮助)。这就是我最终得到的:

Modal.all_of({visibility: true}, {"$or" => query})

这似乎可以正确返回“可见性:true”以及我或选择器中的所有内容:

query = [{:songs.gte => 1},{:books.gte => 1}]