Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 执行一个Mongoid查询,而不是两个单独的Mongoid查询_Ruby On Rails_Mongodb_Mongoid - Fatal编程技术网

Ruby on rails 执行一个Mongoid查询,而不是两个单独的Mongoid查询

Ruby on rails 执行一个Mongoid查询,而不是两个单独的Mongoid查询,ruby-on-rails,mongodb,mongoid,Ruby On Rails,Mongodb,Mongoid,我试图用一个mongoid查询执行以下两个mongoid查询。我试图在Mongoid文档中寻找解决方案,但没有找到任何解决方案 问题1 Event.where(:start_time.gte => current_date.to_s, :start_time.lte => date_after_2_weeks.to_s) 问题2 Event.where(:stop_time.gte => current_date.to_s, :stop_time.lte => date

我试图用一个mongoid查询执行以下两个mongoid查询。我试图在Mongoid文档中寻找解决方案,但没有找到任何解决方案

问题1

Event.where(:start_time.gte => current_date.to_s, :start_time.lte => date_after_2_weeks.to_s)
问题2

Event.where(:stop_time.gte => current_date.to_s, :stop_time.lte => date_after_2_weeks.to_s)
有没有办法只使用一个查询来执行上述两个单独的查询?请帮忙。我将非常感激

谢谢, -Parash-

只要从底层驱动程序中提取,就可以使用“原始”和真正的Mongo语法

“联盟”基本上是指:

“交叉点”基本上是指:

你可能会发现这是隐含的。因此不需要特定的操作数。字段名毕竟是不同的


也许有一种“railsy”方法可以做到这一点,但是特定的语法总是有效的。

如果您解释一下这两个查询的要点,可能会有所帮助。您想要两个结果集的组合还是“并集”?还是你在追求结果的共同点或交叉点?谢谢你的回复,尼尔。我想要两个结果集的并集,然后“不行,琼斯博士”。两个结果集的联合只能通过两个查询实现(最好是在您的语言和库支持的情况下并行实现)。MongoDB不执行SQL类型的“union”查询或任何其他形式的“join”,谢谢Neil。我不知道。我感谢你的反馈。但是,如果我想要结果的“交叉点”呢?但是通过Moped(即
Event.collection
)只会给你一堆散列,因此它对查询没有太大的帮助。谢谢大家的帮助。$or查询完成了任务。正如@kuadrosx所建议的,我将它与Event.where()一起使用。
Event.collection.find({
    "$or" => [
        { "start_time" => { 
            "$gte" => current_date.to_s, 
            "$lte" => date_after_2_weeks.to_s
        },
        { "stop_time" => 
            "$gte" => current_date.to_s,
            "$lte" => date_after_2_weeks.to_s
        }
    ]
})
Event.collection.find({
    "start_time" => { 
        "$gte" => current_date.to_s, 
        "$lte" => date_after_2_weeks.to_s
    },
    "stop_time" => {
        "$gte" => current_date.to_s,
        "$lte" => date_after_2_weeks.to_s
    }
})