我想通过传递开始日期和结束日期来查找在给定时间段内可用的每个房间,在这两个测试之一中至少失败一次的每个集合都需要从查找查询中完全排除.
目前看来,如果至少有一个集合成功匹配这些条件之一,它就会返回给我。
这是一个集合的样本
"resa": [
{
"_id": "5cf2a38372373620263c84f1",
"start": "2019-06-01T15:23:00.000Z",
"end": "2019-06-01T16:23:00.000Z"
},
{
"_id": "5cf2a3a772373620263c84f2",
"start": "2022-03-05T16:23:00.000Z",
"end": "2022-03-05T17:23:00.000Z"
}
]
这是我到目前为止的尝试
$or: [
{ resa: { $all: [{ $elemMatch: { start: { $lt : req.query.start }, end: { $lt : req.query.start } } } ]} },
{ resa: { $all: [{ $elemMatch: { start: { $gt : req.query.end }, end: { $gt : req.query.end } } } ]} }
],
源自 Mickl 的答案,我已经尝试过,但没有显示任何结果
Post.find({
capacity: { $gte : req.query.capacity },
$expr: {
$allElementsTrue: {
$map: {
input: "$resa",
in: {
$or: [
{
$and: [
{ $gte: [ "$$this.start", req.query.end ] },
{ $gte: [ "$$this.end", req.query.end ] }
]
},
{
$and: [
{ $te: [ "$$this.start", req.query.start ] },
{ $lte: [ "$$this.end", req.query.start ] }
]
},
{ resa: [] },
]
}
}
}
}
},
我还尝试通过查找不符合条件的集合来反转查询,这意味着它们在给定时间段内不可用
resa: {
$elemMatch: {
$not: {
$or: [
{
$and: [
{ start: { $gte : req.query.start }},
{ start: { $lte : req.query.end } }]
},
{
$and: [
{ end: { $lte : req.query.end }},
{ end: { $gte : req.query.start } }]
},
],
},
},
},
最佳答案
我设法找到了解决方案:
resa: {
$not: {
$elemMatch: {
$or: [
{
$and: [
{ start: { $gte : req.query.start }},
{ start: { $lte : req.query.end } }]
},
{
$and: [
{ end: { $gte : req.query.start }},
{ end: { $lte : req.query.end } }]
},
{
$and: [
{ start: { $gte : req.query.start }},
{ end: { $lte : req.query.end } }]
},
{
$and: [
{ start: { $lte : req.query.start }},
{ end: { $gte : req.query.end } }]
},
],
},
},
},
要理解的重要部分是组装
resa: {
$not: {
$elemMatch: {
$or: [
{
$and: [
{ x: y},
{ x: y }]
},
{
$and: [
{ x: y},
{ x: y }]
},
...
意思有点困惑:“我想找到所有不匹配{this and this}的集合或 {那个和那个}”
关于javascript - mongo 仅当每个元素都符合条件时才查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56414152/
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
查看我的Ruby代码:h=Hash.new([])h[0]=:word1h[1]=h[1]输出是:Hash={0=>:word1,1=>[:word2,:word3],2=>[:word2,:word3]}我希望有Hash={0=>:word1,1=>[:word2],2=>[:word3]}为什么要附加第二个哈希元素(数组)?如何将新数组元素附加到第三个哈希元素? 最佳答案 如果您提供单个值作为Hash.new的参数(例如Hash.new([]),完全相同的对象将用作每个缺失键的默认值。这就是您所拥有的,那是你不想要的。您可以改用
本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决
我是HanamiWorld的新人。我已经写了这段代码:moduleWeb::Views::HomeclassIndexincludeWeb::ViewincludeHanami::Helpers::HtmlHelperdeftitlehtml.headerdoh1'Testsearchengine',id:'title'hrdiv(id:'test')dolink_to('Home',"/",class:'mnu_orizontal')link_to('About',"/",class:'mnu_orizontal')endendendendend我在模板上调用了title方法。htm
我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS
除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p