聚合管道 $set a value to boolean $and is true or false

Aggregation pipeline $set a value to boolean whether $and is true or false

在我的聚合管道中,如果匹配多个条件,我会尝试将一个值设置为 true。稍后我将添加更多 $and 个条件,但它甚至不能用于一个条件。

这些是我尝试过的东西:

    {
      $set: {
        someValue: {
          $and: [
            {
              'ratePlans.maxAdvBookDays': {
                $gte: advBookDays,
              },
            },
          ],
        }
      }
    }
    {
      $set: {
        someValue: {
          $and: [
            {
              $toBool: {
                'ratePlans.maxAdvBookDays': {
                  $gte: advBookDays,
                },
              },
            }
          ],
        }
      }
    }
    {
      $set: {
        someValue: {
          $cond: {
            if: {
              $and: [
                {
                  'ratePlans.maxAdvBookDays': {
                    $gte: advBookDays,
                  },
                },
              ],
            },
            then: 'then',
            else: 'else',
          }
        }
      }
    }

在每种情况下,我都会收到以下错误:

MongoServerError: Invalid $set :: caused by :: FieldPath field names may not contain '.'.

但是,在对布尔值进行硬编码时, 似乎有效:

    {
      $set: {
        someValue: {
          $and: [
            true, true, false, true
          ],
        }
      }
    },
db.collection.aggregate([
  {
    $set: {
      someValue: {
        $cond: {
          if: {
            $and: [
              {
                $gte: [ "$ratePlans.maxAdvBookDays", 2 ]
              }
            ]
          },
          then: "then",
          else: "else"
        }
      }
    }
  }
])

mongoplayground