【问题标题】:mongo find only if every element match the conditionmongo 仅在每个元素都符合条件时才查找
【发布时间】:2019-10-18 05:29:43
【问题描述】:

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

目前看来,如果至少有一个集合成功匹配这些条件之一,它就会返回给我。

这里是一个集合的样本

"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 } }]
              },
            ],
          },
        },
      },

【问题讨论】:

    标签: javascript database mongodb mongoose mongodb-query


    【解决方案1】:

    我设法找到了解决方案:

          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}的集合 {那个那个}”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多