Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Javascript mongo仅当每个元素都匹配条件时才查找_Javascript_Database_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

Javascript mongo仅当每个元素都匹配条件时才查找

Javascript mongo仅当每个元素都匹配条件时才查找,javascript,database,mongodb,mongoose,mongodb-query,Javascript,Database,Mongodb,Mongoose,Mongodb Query,我希望通过传递开始日期和结束日期来查找给定时间段内可用的每个房间,每个在这两个测试中至少失败一次的集合都需要从find查询中完全排除 目前看来,如果至少有一个集合成功匹配了其中一个条件,它就会返回给我 这是一个收集的样本 "resa": [ { "_id": "5cf2a38372373620263c84f1", "start": "2019-06-01T15:23:00.000Z", "end": "2019-06-01T16:23:00

我希望通过传递开始日期和结束日期来查找给定时间段内可用的每个房间,每个在这两个测试中至少失败一次的集合都需要从find查询中完全排除

目前看来,如果至少有一个集合成功匹配了其中一个条件,它就会返回给我

这是一个收集的样本

"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 } } } ]} }
      ],
根据米克尔的回答,我试过这样做,但没有结果

  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 }]
              },
...
这在意思上有点混乱:“我想找到所有与{thisthis}{thatthat}不匹配的集合”